jcropp
jcropp

Reputation: 1246

What is the best way to get parameters from a route in zf2

There are multiple ways to get parameters from a route in zf2. A couple include

$pageID = (int)$this->getEvent()->getRouteMatch()->getParam('pageID');

and

$pageID = (int) $this->params()->fromRoute('pageID', 0);

Which is the best, and why?

Upvotes: 0

Views: 48

Answers (1)

mkempf
mkempf

Reputation: 51

$pageID = (int) $this->params()->fromRoute('pageID', 0);

This is the controller action helper call and should be used inside your controller. Inside the helper

$this->getEvent()->getRouteMatch()->getParam()

is called.

As u can see - both ways are legit - the helper call is, as the name says, a tool to spare you some typing.

You can use the first way to get parameters inside an attached event for example, most of the time used when u attach something to the default ZF2 events like dispatch, render ...

greetings

Upvotes: 1

Related Questions