1

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:

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.

3
  • 2
    The config looks good; are you sure it's a 404 error? You can get a 'route not found' when you use the URL view helper with an invalid route name e.g $this->url('user-manager') rather than $this->url('users/user-manager') Commented Mar 29, 2016 at 20:52
  • @AlexP it's not a 404 error, it seems that with the URL /users/user-manager the application can not find the route named 'user-manager' whilst with /users/user-manager/edit/1 it works fine. Thanks. Commented Mar 30, 2016 at 15:53
  • @AlexP thanks!!! You were right Commented Mar 31, 2016 at 8:28

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

I think the issue is with constraints and default values. Try to remove constraints in user-manager route and see what happen.

1 Comment

Thanks for your reply, I've tried to remove constraints but the issue is still the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.