zf2 routing is not working - php

My root url is http://restaurent.local
i want to route like this http://restaurent.local/menuedit/test/1. But this not working
This is my code
'menuedit' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/menuedit[/:action][/:id]',
'defaults' => array(
'controller' => 'Menu\Controller\Menu',
'action' => 'menuedit',
),
),
),
if any one know about this please help me.

You're attempting to use a Literal route type. You need a Segment route type if you want to match your route parameters ...
'menuedit' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/menuedit[/:action][/:id]',
'defaults' => array(
'controller' => 'Menu\Controller\Menu',
'action' => 'menuedit',
),
),
),
Please consider reading the manual to understand the differences and how they're meant to be used -> http://framework.zend.com/manual/2.3/en/modules/zend.mvc.routing.html#http-route-types

You might also want to set your constraints for the route parameters. I'd also make the trailing backslash optional with [/] otherwise it won't match /route/to/ it would only match /route/to
'menuedit' => array(
'type' => 'segment',
'options' => array(
'route' => '/menuedit[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => [0-9]*'
),
'defaults' => array(
'controller' => 'Menu\Controller\Menu',
'action' => 'menuedit',
),
),
),

I Literal route can't handle params.
'type' => 'Zend\Mvc\Router\Http\Literal',
You want to edit a specific menu, with specific id, this is a param.
For handle param you have to use Segment type.
Don't forget to add constraints option for your action AND your id. More secure.

Related

how to create an optional route parameter for zend framework 2 restful route

I'm working with zend framework 2 and I need to create an optional parameter for a route segment that also has two required parameters. Below is a snippet from the module.config.php describing the route. My understanding is that in ZF2 an optional route parameter can be created by using the
[/:param]
which you can see is what I have. It works fine as long as I send the optional param, but when I leave it out the first two params "uname and appname" are appended together into the "uname" constraint. The route is a parent route.
'roles' => array(
'type' => 'segment',
'options' => array(
'route' => '/roles/:uname/:appname[/:locnames]',
'constraints' => array(
'uname' => '[a-zA-Z].+',
'appname' => '[a-zA-Z0-9_-].+',
'locnames' => 'locnames'
),
'defaults' => array(
'controller' => 'Roles/Controller/RolesController'
),
),
),
What am I missing here, I know you can have define optional parameters, but I can't figure out the correct format
Thanks to grizzm0 on #zftalk or helping me with this one. It was a simple regular expressions issue. Removing the dot(.) in the constraints correctly matched the incoming url parameters. So my route now looks like this:
'roles' => array(
'type' => 'segment',
'options' => array(
'route' => '/roles[/:action][/uname/:uname][/appname/:appname][/locnames/:locnames]',
'constraints' => array(
'uname' => '[a-zA-Z]+',
'appname' => '[a-zA-Z0-9_-]+',
'locnames' => 'locnames'
),
'defaults' => array(
'controller' => 'Roles/Controller/RolesController'
),
),
),
'roles' => array(
'type' => 'segment',
'options' => array(
'route' => '/roles[/:action][/uname/:uname][/appname/:appname][/locnames/:locnames]',
'constraints' => array(
'uname' => '[a-zA-Z].+',
'appname' => '[a-zA-Z0-9_-].+',
'locnames' => 'locnames'
),
'defaults' => array(
'controller' => 'Roles/Controller/RolesController'
),
),
),
You can configure your route like this way.
Here inside your roles controller
you have some action live index.
so your route will be
siteurl/roles/index/uname/john/appname/stackexchange/locanames/yourlocanames
here if you don't want to write appname then remove youre paramater so your route will work.

ZF2 - Language Routes

I need to create a router configuration in Zend Framework 2 like:
http://www.example.com/en
http://www.example.com/fr
http://www.example.com/es
Then the route of the modules like my "cms" module must match:
http://www.example.com/en/cms/test.html (english version)
http://www.example.com/fr/cms/test.html (french version)
http://www.example.com/es/cms/test.html (spanish version)
of course this link must match the english default language:
http://www.example.com/cms/test.html (english version)
I have already used the SimLocale without success because it doesn't handle the asset links as well. So I prefer to handle the language selection manually by a simple parameter for all my custom modules.
The question has been solved on #zftalk IRC today (thanks Michelangelo to contribute back based one what we explained to you...).
To solve the problem, we had to have a look at the defined routes:
'router' => array(
'routes' => array(
'language' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:lang]',
'defaults' => array(
'__NAMESPACE__' => 'Cms\Controller',
'controller' => 'Index',
'action' => 'page',
'lang' => 'en',
'slug' => 'homepage'
),
'constraints' => array(
'lang' => '[a-z]{2}'
)
),
'may_terminate' => true,
'child_routes' => array(
'list' => array(
'type' => 'Segment',
'options' => array(
'route' => '/cms',
'defaults' => array(
'__NAMESPACE__' => 'Cms\Controller',
'controller' => 'Index',
'action' => 'index',
'page' => 1
),
),
),
'page' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:slug].html',
'constraints' => array(
'slug' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'action' => 'page',
),
),
),
'search' => array(
'type' => 'Segment',
'options' => array(
'route' => '/search/[query/:query]',
'constraints' => array(
'query' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'action' => 'search',
'query' => null,
),
),
),
'paginator' => array(
'type' => 'Segment',
'options' => array(
'route' => '/list/[page/:page]',
'constraints' => array(
'page' => '[0-9]*',
),
'defaults' => array(
'page' => 1,
),
),
),
),
),
Then, the updated description of the problem was the following:
mikunos: http://www.example.com/en (homepage) this is ok
mikunos: http://www.example.com/en/cms (cms list) this is ok
mikunos: http://www.example.com/en/cms/about.html (page) this not
Which have led us on changing the 'page' route as a child route of 'list' (/cms).
Then the discussion finished with indications on what to do to setup the default language if none have been selected:
mikunos: as you have seen I need two main routes
mikunos: the first one is /lang/module and the second one is /module
mikunos: so I have to create two branches
mikunos: right?
tdutrion: you may want to use an url rewriting or something here
tdutrion: I would go for a redirect 301 from /module to /en/module so you have no duplicate content
tdutrion: and then no route can be accessed without a language
mikunos: ok
tdutrion: you can do it different ways, but I think the best one would be creating a event listener and do that before routing
tdutrion: you can also do it in your .htaccess of you are using apache, but then what if you migrate to IIS or Ngnix or else...
mikunos: interesting
tdutrion: again I believe Jurian Sluiman’s code does that
You can use subdomains for each language you need witch I suppose you don't want or you really don't need (eg. /en) to change the language of your application and/or web site. Is mandatory to use /en, /fr, etc.?

How to get params in action in ZendFramework 2?

I have configured a route in my module.config.php to handle two parameters ,
here is the content of the file module.config.php , i put just the definition of the routes ( same routes ) :
updated :
'router' => array(
'routes' => array(
'Products' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/application/admin/products[/:id]',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Admin',
'action' => 'index',
),
),
),
'productsList' => array(
'type' => 'segment',
'options' => array(
'route' => '/application/products/productsList[/:type][/:id]',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Products',
'action' => 'productsList'
),
),
),
'Emplacement' => array(
'type' => 'segment',
'options' => array(
'route' => '/application/support/listeEmplacementsSupport[/:pkSupport]',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Support',
'action' => 'listeEmplacementsSupport',
),
),
),
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id][/:dr]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
And I build a link in the view using the following
<a href="/application/products/productsList/mandan/<?php echo $coreg['idProduct']; ?>" >
<img height='20' src="/img/mail-recevoir-32.png" alt='products' />
</a>
To get one parameter I use
$param = $this->params('id');
Routes are matched in the 'Last in first out' LIFO order.
The relevant section of the documentation states:
Typically, routes should be queried in a LIFO order, and hence the reason behind the name RouteStack. [...] you will need to ensure that routes that potentially overlap are registered such that the most specific match will match first (i.e., register later)
Therefore based on you current configuration, as all the routes are register at the top level, the last route (application/default) is being checked and then subsequently matched first, before the productList is given a chance.
Also because application/default defines an optional id parameter as the first argument this is the reason that you get the value of the type param when using $this->params('id').
Soultion
The easiest solution is just to reorder the routes; moving the application route right to the top of the definition should work.
The order should be the following
'router' => array(
'routes' => array(
'application' => array(
// .. same
'child_routes' => array(
'default' => array(
// .. Same
),
),
),
'Products' => array(
// ..same
),
'productsList' => array(
// ..same
),
'Emplacement' => array(
// ..config
),
),
),
You could alternatively give each route a priority, regardless of the order, however I think It would make more sense to keep them in a natural order to save confusion in the future.
While I'm here
I mentioned in my comment you incorrectly used a Literal route rather than a Segment. You have this same issue with the the Products route definition. If a route is literal it will only match on the exact value (so you would need /[:id] in the URL).
/application/admin/products[/:id]
Therefore you will also need to update this route to a segment in order for the product id to be regarded as a parameter.
Lastly, there is a easier way of constructing URL's in the view, using the URL view helper. Your view script code would be changed to
<?php
$type = 'mandan';
$id = $coreg['idProduct'];
?>
<a href="<?php echo $this->url('productsList', compact('type', 'id')); ?>">
<img height='20' src="/img/mail-recevoir-32.png" alt='products' />
</a>

Zend: How to add/get URL parameters to a controller action

I have a /files controller that will have two actions: upload and download
it's defined as follows:
'files' => array(
'type' => 'Segment',
'options' => array(
'route' => '/files[/:action]',
'defaults' => array(
'controller' => 'Application\Controller\Files',
'action' => 'index',
),
),
),
I want the download action to be accessed like /files/download/1?authString=asdf. 1 in this case is a fileId.
I understand I can just change the route to /files[/action[/:fileId]] to set up the route, correct me if I'm wrong, but then how do I access the fileId inside the downloadAction? And is there anything else I need to change about the route definition to make it work??
I can just change the route to /files[/action[/:fileId]] to set up the route, correct me if I'm wrong
You're not wrong, that would be a valid route.
is there anything else I need to change about the route definition?
If you add the fileId as a optional route param then you will need to do some manual checking within the downloadAction() to ensure it is set.
Another solution would be to separate the routes into children, this ensures that unless you have the correct parameters on each route, it would not be matched.
'files' => array(
'type' => 'Segment',
'options' => array(
'route' => '/files',
'defaults' => array(
'controller' => 'Application\Controller\Files',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'download' => array(
'type' => 'Segment',
'options' => array(
'route' => '/download/:fileId',
'defaults' => array(
'action' => 'download',
),
'constraints' => array(
'fileId' => '[a-zA-Z0-9]+',
),
),
),
'upload' => array(
'type' => 'Literal',
'options' => array(
'route' => '/upload',
'defaults' => array(
'action' => 'upload',
),
),
),
),
),
how do I access the fileId inside the downloadAction
The easiest way would be to use the Zend\Mvc\Controller\Plugin\Params controller plugin to fetch the parameter from the route).
// FilesController::downloadAction()
$fileId = $this->params('fileId');
Or specifically from the route
// FilesController::downloadAction()
$fileId = $this->params()->fromRoute('fileId');

Redirecting for child routes in ZF2 throws an error

I've have an problem to redirect on child routes in zend framework 2. I can access the controller and action but while redirecting it throws me an error missing parameter "id".
'admin' => array(
'type' => 'segment',
'options' => array(
'route' => '/admin[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Admin\Controller\Admin',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'settings' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/general[/][:action][/][:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Admin\Controller\Settings\General',
'action' => 'index',
),
),
),
),
),
I have given redirect to route like below,
return $this->redirect()->toRoute('admin/settings');
But it throws an error,
Missing parameter "id"
as the error message implies you need to add the "id" parameter. You can redirect with a parameter like so.
return $this->redirect()->toRoute('admin', array('action'=>'settings', 'id' => $id));
You did not show us your Controller action's but I assume 'settings' is a action within your admin module.
At this point I cannot really see what kind of id the admin/settings function need's might aswell just try to add a 0 or 1 to try the route at first for testing purposes.
The route matching method doesn't seem to 'consume' the parent node of the route.
Changing the child route to include the /admin part of the grammar to
/admin/settings/general[/][:action][/][:id]
or
/admin/settings[/][:action][/][:id]
should allow $this->redirect()->toRoute('admin/settings'); to work.

Categories