Zend framework routing error - php

I have a problem with a routing in Zend framework.
'name' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/site/:id/orders[/:page]',
'constraints' => array(
'id' => '[0-9]*',
'page' => '[0-9]*'
),
'defaults' => array(
'controller' => 'Application\Controller\Site',
'action' => 'action'
),
),
),
And in a controller.
$id = (int) $this->params()->fromRoute('id');
And in some (!) cases a browser returns this error - "Missing parameter 'id'", but I don't know why.
Can anybody help me on this issue?

well based on your route configuration id must exist in your routes, so the link you requested didn't have id . and your constrains also should change to 'id' => '[0-9]+' so the id must exist.
and also you get the id in the controller by just typing
$id=$this->params("id");
which will get the id too

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.

How to pass parameters by url with Zend Framework 2

I'm getting into trouble with passing param by url in Zend Framework 2. For example : I want to access page by a URL like this:
http://example.com/student/details/aaa it's working, but if my url like this :
http://example.com/student/details/1 it's not working, And i still get a 404.
I tried to follow the instruction from how to pass params using a route in Zend Framework 2?
But it still doesn't work.
here's my module.config.php
'student' => array(
'type' => 'segment',
'options' => array(
'route' => '/student[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'__NAMESPACE__' => 'AppFeeder\Controller',
'controller' => 'Student',
'action' => 'index',
),
),
),
And i use this $this->getEvent()->getRouteMatch()->getParam('id') in my controller.
Can anyone help me ?
You are using wrong route rule for id which says start from alphabet then alphabet+number+"_-".
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
Instead you need to change the rule that says start from alphabet+number then alphabet+number+"_-"
'id' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',

Zend 2 redirect with toRoute not working

Why is Zend 2 such a !##(#(!##??
OK, so I'm trying to get a simple redirect working. I have a controller called 'listitems' with an action called 'editlistitem'. After hours of banging on it with a hand sledge, I've finally got the form to work and the validation to work and the hydration to Doctrine to work so I can save the result.
The last step is to redirect the user to the 'showlistitem' action which includes the id trailing it. (full route sub path is 'listitem/showlistitem/2' where 2 is the id I want to see)
I have tried:
$this->redirect()->toRoute('/listitem/showlistitem/2');
$this->redirect()->toRoute('listitem/showlistitem/2');
$this->redirect()->toRoute('showlistitem/2');
$this->redirect()->toRoute('listitem/showlistitem', array('id' => 2));
$this->redirect()->toRoute('listitem-showlistitem', array('id' => 2));
None of them flippin work! (they all return route not found)
A route to the controller is in modules.config.php with a child route to the action. I can go directly to the url by typing it in manually and it works fine. How in the bleep do I get Zend to redirect the user to that route from an action?
The toRoute method provided by the The Redirect plugin needs the route name to be passed as parameter. This is its desciption :
toRoute(string $route = null, array $params = array(), array $options = array(), boolean $reuseMatchedParams = false)
Redirects to a named route, using the provided $params and $options to assembled the URL.
Given this simple route configuration example :
//module.config.php
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Segment',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'index',
'action' => 'index',
),
),
),
'app' => array(
'type' => 'Literal',
'options' => array(
'route' => '/app',
'defaults' => array(
'controller' => 'index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id'=>'[0-9]+',
),
),
),
),
),
),
),
This redirection works :
return $this->redirect()->toRoute('app/default',
array('controller'=>'controller-name', 'action'=>'action-name', 'id'=>$id));
In your case, this would work :
return $this->redirect()->toRoute('app/default',
array('controller'=>'listitem', 'action'=>'showlistitem', 'id'=>2));

zf2 routing is not working

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.

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