How to Access Route, Post, Get etc. Parameters in Zend Framework 2

How to access route, post, get etc. parameters in Zend Framework 2

The easiest way to do that would be to use the Params plugin, introduced in beta5. It has utility methods to make it easy to access different types of parameters. As always, reading the tests can prove valuable to understand how something is supposed to be used.

Get a single value

To get the value of a named parameter in a controller, you will need to select the appropriate method for the type of parameter you are looking for and pass in the name.

Examples:

$this->params()->fromPost('paramname');   // From POST
$this->params()->fromQuery('paramname'); // From GET
$this->params()->fromRoute('paramname'); // From RouteMatch
$this->params()->fromHeader('paramname'); // From header
$this->params()->fromFiles('paramname'); // From file being uploaded

 

Default values

All of these methods also support default values that will be returned if no parameter with the given name is found.

Example:

$orderBy = $this->params()->fromQuery('orderby', 'name');

When visiting http://example.com/?orderby=birthdate,
$orderBy will have the value birthdate.

When visiting http://example.com/,
$orderBy will have the default value name.

 

Get all parameters

To get all parameters of one type, just don't pass in anything and the Params plugin will return an array of values with their names as keys.

Example:

$allGetValues = $this->params()->fromQuery(); // empty method call

When visiting http://example.com/?orderby=birthdate&filter=hasphone $allGetValues will be an array like

array(
'orderby' => 'birthdate',
'filter' => 'hasphone',
);

 

Not using Params plugin

If you check the source code for the Params plugin, you will see that it's just a thin wrapper around other controllers to allow for more consistent parameter retrieval. If you for some reason want/need to access them directly, you can see in the source code how it's done.

Example:

$this->getRequest()->getRequest('name', 'default');
$this->getEvent()->getRouteMatch()->getParam('name', 'default');

NOTE: You could have used the superglobals $_GET, $_POST etc., but that is discouraged.

Pass parameters by URL in Zend Framework 2

I've solved the problem!

You should change the function as following:

public function updateAction()
{
$paramName = $this->getEvent()->getRouteMatch()->getParam('id');

// Do something with $paramName

return array();
}

There're more methods to get parameters in Zend Framework2:
How to access route, post, get etc. parameters in Zend Framework 2

ZF2: Get url parameters in controller

From here:

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

More documentation here, but it looks kind of incomplete.

Dynamically change default route parameters in zend framework 2?

I had a similar problem and figured it out, try:

$e->getRouter()->setDefaultParam('lang', 'de_DE');

I'm triggering this on MvcEvent::EVENT_DISPATCH (see update note below) with the use of a listener, but onBootstrap in Module.php should work too.

Update:

Ok, now I see that MvcEvent::EVENT_DISPATCH is too late for applying a default parameter to the Router. Especially when You're interested not only in passing the language via route, but also in having translatable routes (in conjunction with 'router_class'=>'Zend\Mvc\Router\Http\TranslatorAwareTreeRouteStack').

So it should be on MvcEvent::EVENT_ROUTE:

// applying a default language param to route
$e->getRouter()->setDefaultParam('lang', 'de_DE');

// Now detect the requested language or retrieve
// from matched route
// $detectedLocale =...
// ...

// Retrieve the translator
$sm->get('translator');

// Apply detected locale to the translator
$translator->setLocale($detectedLocale);

// and now this apply the translator to the router
// for translatable routes
$e->getRouter()->setTranslator($translator);

// but don't forget about
// 'router_class'=>'Zend\Mvc\Router\Http\TranslatorAwareTreeRouteStack'
// for translatable routes

I see people saying, that You should do this in onBootstrap(), but IMVHO onBootstrap is TOO EARLY for retrieving a matched route, which is required for detecting the locale/language passed by the client in a route/url parameter.

By saying "detecting the locale" I'm definitely not thinking about any dirty string operations on the url/query string, I'm thinking about a clean getParam() on the matched route.

Related:
http://framework.zend.com/manual/2.2/en/modules/zend.mvc.mvc-event.html

Zend Framework 2 can´t receive HTTP Post - get was unable to fetch or create an instance for fromPost

Two changes are needed. First, as Tim wrote, it should be

$cl_id = $this->params()->fromPost('checklist');

Second, form items are passed to the controller by name, not id. So in addition to the id field on the element, you need a name field, as follows:

<input type="hidden" id="checklist" name="checklist" value="<?php echo $index;?>">

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

$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



Related Topics



Leave a reply



Submit