How to access a routed parameter from inside the controller?

This is the route ...

'panel-list' => array ( 'type' => 'Segment', 'options' => array ( 'route' => '/panel-list/:pageId[/:action]', 'constraints' => array ( 'pageId' => '[a-z0-9_-]+' ), 'defaults' => array ( 'action' => 'index', 'controller' => 'PanelList', 'site' => null ), ), ), 

What I need to add here ....

 public function indexAction() { echo ??????? } 

to repeat page id?

0
source share
3 answers

In beta5 zf2, it has changed to be easier to use, so you don’t need to remember different syntaxes for each other type. i cite :

New Params controller plugin. Allows you to receive a request, message, cookie, header and route parameters. Using $ this-> params () -> fromQuery ($ name, $ default).

So, to get the parameter from the route, all you have to do is.

 $param = $this->params()->fromRoute('pageId'); 

This can also be done with the request ($ _GET) and post ($ _POST), etc., as the quote says.

 $param = $this->params()->fromQuery('pageId'); // will match someurl?pageId=33 $param = $this->params()->fromPost('pageId'); // will match something with the name pageId from a form. // You can also set a default value, if it empty. $param = $this->params()->fromRoute('key', 'defaultvalue'); 

Example:

 $param = $this->params()->fromQuery('pageId', 55); 

if url is someurl? pageId = 33 $ param will contain the value 33. if the URL does not have? pageId $ param will contain a value of 55

+3
source

You tried

 $this->getRequest()->getParam('pageId') 
+2
source

$ this-> GetEvent () β†’ getRouteMatch () β†’ GetParam ('PageId');

+2
source

Source: https://habr.com/ru/post/922180/


All Articles