Routing issues and timeout in Zend Framework 2 - php

I'm trying to achieve the routing explained in below but having a lot of trouble (the documentation is like a foreign language). Should routing for each module be kept separate in that module's config file?
"route": Module/Controller/Action
"/": Application/Index/index
"/:example": Application/Campaign/index (with param "campaign" = [example])
"/admin": Admin/Admin/index
"/admin/login": Admin/Access/login
"/admin/:controller/:action": Admin/[defined]/[defined]
I've tried to understand and use the skeleton application to do this and everything works up to the "/admin/controller/action" route where the script times out (I'm guessing there's a recursive loop in there somewhere). My route definitions are:
'home' => array(
'type' => 'literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
'campaign' => array(
'type' => 'segment',
'options' => array(
'route' => '[/:campaign]',
'constraints' => array(
'campaign' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\Campaign',
'action' => 'index',
'campaign' => ''
),
),
),
'admin' => array(
'type' => 'segment',
'options' => array(
'route' => '/admin',
'defaults' => array(
'controller' => 'Admin\Controller\Admin',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),

1) Yes, you should separate routing config to modules, ZF2 will merge it automatically. So you need to store home and campaign routes in Application/config/module.config.php and admin route in Admin/config/module.config.php
2) I don't see any global mistakes in your config. Two small things:
2.1) You can use literal instead of segment for admin parent route
2.2) If you want to use :controller as a route param, be sure to make shortcut for it via invokables section of config. For example:
'controllers' => array(
'invokables' => array(
'index' => 'Application\Controller\Index'
'admin' => 'Admin\Controller\Admin'
),
),
Now, for example, url /admin/admin/update is connected with Admin\Controller\Admin->updateAction()
3) I think it's not possible to make some recursive loops using only routing config. So check your code in controllers/services

Related

Zend Framework 2 getting URL from route

What I'm trying to do is generate a URL using the url view helper in Zend Framework 2
The URL that I am trying to generate should be
my-website.com/app/## (where ## is equal to the "ownerID")
What happens when I generate this using the view helper like this:
$this->url('mymodule', array('action'=>'show', 'ownerID'=>'28'));
is it only generates "my-website.com/app", but I am expecting "my-website.com/app/28" based on the routing configuration.
Here is the route information from my module.config.php file
'router' => array(
'routes' => array(
'mymodule' => array(
'type' => 'segment',
'options' => array(
'route' => '/app',
'defaults' => array(
'controller' => 'MyModule\Controller\MyModule',
'action' => 'show',
),
),
// Defines that "/app" can be matched on its own without a child route being matched
'may_terminate' => true,
'child_routes' => array(
'archive' => array(
'type' => 'segment',
'options' => array(
'route' => '/:action[/:ownerID[/:clientID]]',
'defaults' => array(
),
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'ownerID' => '[0-9]+',
'clientID' => '[0-9]+',
)
),
),
'single' => array(
'type' => 'segment',
'options' => array(
'route' => '/:ownerID[/:clientID]',
'defaults' => array(
'action' => 'show',
),
'constraints' => array(
'ownerID' => '[0-9]+',
'clientID' => '[0-9]+',
)
),
),
)
),
)
),
The same behavior occurs when using $this->redirect()->toRoute.
All of the routes work as expected when you type them manually, it is just the generation of the URL that has me stumped.
To more directly answer the question, it doesn't do what you expect because you are only passing the top level route name ("mymodule") to the URL helper. The ownerID is part of a child route.
What you actually want is:
$this->url('mymodule/archive', array('action'=>'show', 'ownerID'=>'28'));
(or possibly mymodule/single, depending on the output you want).

Php Zend 2 Route not found

I'm creating a new small application with Zend Framework 2 and I've some problem with routes...
I've created a a new module cloning the Skeleton Module with Git and renaming it "Users". I've added controllers to register a new user, to login and to perform CRUD operations.
This is my module folder structure:
and this is my module.config.php file:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Users\Controller\Index' => 'Users\Controller\IndexController',
'Users\Controller\Register' => 'Users\Controller\RegisterController',
'Users\Controller\Login' => 'Users\Controller\LoginController',
'Users\Controller\UserManager' => 'Users\Controller\UserManagerController',
),
),
'router' => array(
'routes' => array(
'users' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/users',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Users\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This route is a sane default when developing a module;
// as you solidify the routes for your module, however,
// you may want to remove it and replace it with more
// specific routes.
'login' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/login[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Users\Controller\Login',
'action' => 'index',
),
),
),
'register' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/register[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Users\Controller\Register',
'action' => 'index',
),
),
),
'user-manager' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/user-manager[/:action[/:id]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Users\Controller\UserManager',
'action' => 'index',
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'users' => __DIR__ . '/../view',
),
),
);
When I try with my browser the url test.local/users/login or test.local/users/register everything works fine but if I try test.local/users/user-manager I get the following error:
Route with name "user-manager" not found
whilst with test.local/users/user-manager/edit/5 it is rendered the correct page.
I'm bit confused and I don't know how to solve this problem.
Any help is very appreciated. Thanks in advance.
problem solved! #AlexP was right, the problem was in index.phtl file with $this->url('user-manager');, I've changed in $this->url('users/user-manager') and now it works.
Thanks again!
I think the issue is with constraints and default values. Try to remove constraints in user-manager route and see what happen.

ZF2 - Issues with $this->url and Routing

I'm having and issue with the view helper function this->url() that doesn't return and url from a child route and I get a White Screen of the dead. When I put the url manually in the browser the router recognizes the url well.
I have this in my module.config.php:
'news' => array(
'type' => 'Segment',
'options' => array(
'route' => '/news[/:id]',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Application\Controller\News',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'allnews' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:action[/:tab[/:page]]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'tab' => '[0-9]+',
'page' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Application\Controller\News',
'action' => 'all',
'tab' => 1,
'page' => 1,
),
),
),
),
),
For example:
/news/2
Works correctly and matches the parent route
And:
/news/all/2/5
Works correctly and matches the child route.
But when I use this in the view-helper I get a White Screen of the Dead:
echo($this->url('news/allnews', array('action' => 'all', 'tab' => '1', 'page' => '1')));
My question is: What it's wrong?? I used this method in others views and worked well.
You do not need to use child routes in combination with Router Segments. One Segment should be sufficient!
'news' => array(
'type' => 'Segment',
'options' => array(
'route' => '/news[/:action[/:id][/:tab][/:page]]',
'constraints' => array(
'id' => '[0-9]+',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'tab' => '[0-9]+',
'page' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Application\Controller\News',
'action' => 'index',
),
),
),
As long as the Defaultcontroller is sufficient you for your needs you also would not need to have it within your params. Once you need more then 1 you prob want to have another constraint defining the controller.
Now you could simply use the url viewhelper in various ways.
if you only need the default action within your default controller you could write it like so.
$this->url('news');
If you need to route to actions with params you could use the viewhelper like so
$this->url('news', array('action' => $action, 'tab' => $tab, 'page' => $page));
Ideally I would use a Router Literal with Router Segments as child routes since bombarding the router segment with tons of constraints gets messy pretty fast.
...
'custom_route' => array(
'type' => 'Literal',
'priority' => 1337,
'options' => array(
'route' => '/custom',
'defaults' => array(
'__NAMESPACE__' => 'Custom\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'custom_child' => 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]+',
),
'defaults' => array(
),
),
),
),
),
...
Pretty sure this won't help but everything does look right in your routing... Have you tried
$this->url('news/allnews', array('action' => 'all', 'tab' => 1, 'page' => 1))
Don't know how the routing stuff handles contraints on digits and providing strings to url view helper.
You probably either need to enable errors or look in the log to find out the actually error

ZF2 route with prefix in not working for default

I want add crm as prefix of CRM module.
This is router section in my module.config.php
'router' => array(
'routes' => array(
'calendar' => array(
'type' => 'segment',
'options' => array(
'route' => '/crm/calendar[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Crm\Controller\Calendar',
'action' => 'index',
),
),
),
When i use test.dev/crm/calendar/index is working properly. But it is not working for test.dev/crm/calendar . I couldn't find any issue.
When i use 'route' => '/calendar[/:action][/:id]', i can use test.dev/calendar. But i need use prefix. How can i do it?
I think it might be that you have to add 'may_terminate' => true,
So your route definition will look like this:
'calendar' => array(
'type' => 'segment',
'options' => array(
'route' => '/crm/calendar[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Crm\Controller\Calendar',
'action' => 'index',
),
),
'may_terminate' => true,
),
Try if that works.
Otherwise you can also split it and make crm to a literal route.
'crm' => array(
'type' => 'literal',
'options' => array(
'route' => '/crm',
),
'may_terminate' => false,
'child_routes' => array(
'calendar' => array(
'type' => 'segment',
'options' => array(
'route' => '/calendar[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Crm\Controller\Calendar',
'action' => 'index',
),
),
),
),
),
But this last solution means that you will always have to route to crm/calendar
As seen configs should correct.
Did you tried without last / (test.dev/crm/calendar, not test.dev/crm/calendar/)
Route configuration is correct. This route had over write from another module route. That's the issue. ZF2 has not easy way to check all routes and paths.

Zend Framework 2 - trailing slashes in route

Havin in a Zend Module following structure
Name of Module
Test
config
src
view
layout
test
index
index.phtml
login
index.phtml
register
index.phtml
Module.php
Now when accessing website for example for login file it will be as following http://justsite.something.com/login
Problem Need to change to http://justsite.something.com/login/ for that i added to login folder another folder index and there putted index.phtml file like this
login
index
index.phtml
But that doesn't seem to work. Should i change in LoginController.php for this to work?
module.confing.php
'router' => array(
'routes' => array(
'test' => array(
'type' => 'Literal',
'options' => array(
'route' => '/test',
'defaults' => array(
'__NAMESPACE__' =>'Test\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' =>
'/[:controller[/:action]]',
'constraints' => array(
'controller' =>
'[a-zA-Z][a-zA-Z0-9_-]*',
'action' =>
'[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(),
),
),
),
),
'login' => array(
'type' => 'Literal',
'options' => array(
'route' => '/login/', // PUT HERE WHATEVER YOU WANT
'defaults' => array( // AND MAP THAT URL TO CONTROLLER/ACTION
'__NAMESPACE__' =>'Test\Controller',
'controller' => 'Login',
'action' => 'index',
),
),
),
),
),
Fixed Just added
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' =>
'/[:controller[/:action]/]',
Like #tasmaniski wrote you don't need to make changes to LoginController or whatever, all comes down to your route config. You are not the only one who wants "trailing slashes" in his routes, here you can read how you can achieve this.
Apparently you need to put it between []
'route' => '/[:controller[/[:action[/]]]]',
Hope that solves it for you.
NOTE Some people suggest using a rewrite rule for this, I thought this might be worth mentioning too.
No matter what is your forlder structure. You need to make a change in the config file module.config.php, section "routes".
You only need to put in "route" value with slash at the end and map it to controller/action.
'routes' => array(
'login' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/login/', // PUT HERE WHATEVER YOU WANT
'defaults' => array( // AND MAP THAT URL TO CONTROLLER/ACTION
'controller' => 'Application\Controller\Login',
'action' => 'index',
),
),
),
)

Categories