Zend2 Routing __NAMESPACE__ doesn't work or be ignored - php

I'm using this config as my application module config in Zend2 which is really normal and every one suggested as a standard routing rule:
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
),
),
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
'application' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Zend\Mvc\Router\Http\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(
),
),
),
),
),
),
),
the routing for home works fine. but for http://localhost/application I get:
Index(resolves to invalid controller class or alias: Index)
and for http://localhost/application/index/index I get:
index(resolves to invalid controller class or alias: index)
if I change this:
'application' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
to this:
'application' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
As you know definitely for http://localhost/application it will work fine like home url
if I use this:
'controllers' => array(
'invokables' => array(
'index' => 'Application\Controller\IndexController',
),
),
as you know the configurations will merge and I should have just one index controller in the project.
Why the line '__NAMESPACE__' => 'Application\Controller', be ignored and it looks for just Index or index in the controllers array which no exist??
EDIT:
With comparing to other projects I added this to Application/Module.php :
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
and it works now but I need and explanation. is it the solution? I mean should I add this to one of Module.php files in the project to make routing rules work fine? and why without it the __NAMESPACE__ will be ignored in routing rules?

You already found the solution, adding the ModuleRouteListener is the right thing to do. The explanation can be found in the description of the onRoute method inside this listener:
Listen to the "route" event and determine if the module namespace should be prepended to the controller name.
If the route match contains a parameter key matching the MODULE_NAMESPACE constant, that value will be prepended, with a namespace separator, to the matched controller parameter.

Related

ZF2 route to controller

I have created a controller called "ActividadesPlanificadasController.php" and I have defined in my module.config the next routing but it is not work.
'publico/peticiones-incidencias-planificadas/actividades-planificadas' => array(
'type' => 'Literal',
'options' => array(
'route' => 'publico/peticiones-incidencias-planificadas/actividades-planificadas',
'defaults' => array(
'__NAMESPACE__' => 'Privado\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
//'route' => '/[:controller[/:action[/:id]]]',
'route' => '/[:controller[/:action][/:id/:system]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
'system' => '[a-zA-Z][a-zA-Z0-9_-]*'
//'system' => '[0-9]*'
),
'defaults' => array(
),
),
),
),
),
These are my controllers ...
'controllers' => array(
'invokables' => array(
'Publico\Controller\Index' => Controller\IndexController::class,
'Publico\Controller\Login' => Controller\LoginController::class,
'Publico\Controller\NoAccess' => Controller\NoAccessController::class,
'Publico\Controller\ActividadesPlanificadas' => Controller\ActividadesPlanificadasController::class
),
),
Because I want to access to my controller with the next url: http://gnsys.local/publico/peticiones-incidencias-planificadas/actividades-planificadas
And I've got the next error ...
A 404 error occurred
Page not found.
The requested controller could not be mapped to an existing controller class.
Controller:
Publico\Controller\PeticionesIncidenciasPlanificadas(resolves to invalid controller class or alias: Publico\Controller\PeticionesIncidenciasPlanificadas)
No Exception available
What am I doing wrong?
Edit 1:
I have updated my module.config and it works with the route: http://gnsys.local/publico/actividades-planificadas
module.config:
'publico/actividades-planificadas' => array(
'type' => 'Literal',
'options' => array(
'route' => 'publico/actividades-planificadas',
'defaults' => array(
'__NAMESPACE__' => 'Publico\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
//'route' => '/[:controller[/:action[/:id]]]',
'route' => '/[:controller[/:action][/:id/:system]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
'system' => '[a-zA-Z][a-zA-Z0-9_-]*'
//'system' => '[0-9]*'
),
'defaults' => array(
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'Publico\Controller\Index' => Controller\IndexController::class,
'Publico\Controller\Login' => Controller\LoginController::class,
'Publico\Controller\NoAccess' => Controller\NoAccessController::class,
'Publico\Controller\ActividadesPlanificadas' => Controller\ActividadesPlanificadasController::class
),
),
But I want that my route to access to ActividadesPlanificadasController would be: http://gnsys.local/publico/peticiones-incidencias-actividades/actividades-planificadas
If I change my module.config to ...
'publico/peticiones-incidencias-planificadas/actividades-planificadas' => array(
'type' => 'Literal',
'options' => array(
'route' => 'publico/peticiones-incidencias-planificadas/actividades-planificadas',
'defaults' => array(
'__NAMESPACE__' => 'Publico\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
//'route' => '/[:controller[/:action[/:id]]]',
'route' => '/[:controller[/:action][/:id/:system]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
'system' => '[a-zA-Z][a-zA-Z0-9_-]*'
//'system' => '[0-9]*'
),
'defaults' => array(
),
),
),
),
),
I've got the error
A 404 error occurred Page not found.
The requested controller could not be mapped to an existing controller class.
Controller: Publico\Controller\PeticionesIncidenciasPlanificadas(resolves to
invalid controller class or alias:
Publico\Controller\PeticionesIncidenciasPlanificadas)
No Exception available
What am I still doing wrong?
You point to a controller called 'Privado\Controller\Index' but your invokable is called 'Publico\Controller\Index'.
Change the namespace so it corresponds.
Privado -> Publico
or
Publico -> Privado
or add a controller for 'Privado\Controller\Index'
'invokables' => array(
'Privado\Controller\Index' => //your privado controller,
//...other controllers...
)
UPDATE
Make also sure you have an invokable controller class in the correct namespace. So if you register like this:
'invokables' => array(
'Privado\Controller\Index' => 'Privado\Controller\IndexController'
)
You need a controller class in a php file called IndexController.php in the folder Privado - Controller
- Privado
- Controller
- IndexController.php
And the class should have the correct name and namespace constant:
<?php
namespace = Privado\Controller;
class IndexController extends ... {
}

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.

Zend Framework 2: Using multiple routes with multiple modules

I use two modules in my zend framework 2 application:
Module A
Module B
I encounter the problem that I can only use one of the routes that I have configured for the corresponding module. The route that is used, depends on the sorting of the modules in the application.config.php file:
<?php
return array(
'modules' => array(
'ModuleA','ModuleB'
);
?>
Every module contains nearly the same configuration module.config.php:
<?php
return array(
'router' => array(
'routes' => array(
'ModuleA' => array(
'type' => 'Literal',
'options' => array(
'route' => '/moduleA',
'defaults' => array(
'__NAMESPACE__' => 'ModuleA\Controller',
'controller' => 'index',
'action' => 'index',
),
),
'may_terminate' => false,
'child_routes' => array(
'moduleA-index' => array(
'type' => 'Segment',
'options' => array(
'route' => '/index[/:action]',
'defaults' => array(
'controller' => 'index',
'action' => 'index'
)
)
)
)
)
)
)
);
Current situation:
URL /moduleA routes to /ModuleA/Index/Index
URL /moduleB routes to /ModuleA/Index/Index
Expected:
URL /moduleA routes to /ModuleA/Index/Index
URL /moduleB routes to /ModuleB/Index/Index
Do you have any advice for me how to use both configurations/routes in the right way?
Are you using 'controller' => 'index', in your moduleB config too?
if yes then there is your problem index is on alias and only 1 controller can have that alias, in other words alias's should be unique throw out the Application and not just a module.
define a unique name(alias) for your controller and you will be fine.
in my project i just use the FQN so there is no confusion (Namespace\Controller\ControllerName)
Just add 'priority' param
<?php
return array(
'router' => array(
'routes' => array(
'ModuleA' => array(
'type' => 'Literal',
'priority' => 100, // <-- priority
'options' => array(
'route' => '/moduleA',
'defaults' => array(
'__NAMESPACE__' => 'ModuleA\Controller',
'controller' => 'index',
'action' => 'index',
),
),
'may_terminate' => false,
'child_routes' => array(
'moduleA-index' => array(
'type' => 'Segment',
'options' => array(
'route' => '/index[/:action]',
'defaults' => array(
'controller' => 'index',
'action' => 'index'
)
)
)
)
)
)
)
);

ZF2 - The requested URL could not be matched by routing

i receive this error when i go to
localhost/app/public/spanel/test/index
This application have a sPanel module. Into it have a TestController with indexAction()
This is the sPanel\config\module.config.php
<?php
return array(
'controllers' => array(
'invokables' => array(
'sPanel\Controller\Test' => 'sPanel\Controller\TestController',
),
),
'router' => array(
'routes' => array(
'spanel' => array(
'type' => 'segment',
'options' => array(
'route' => '/test[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'sPanel\Controller\Test',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'spanel' => __DIR__ . '/../view',
),
),
);
First of all i strongly suggest that you start working with virtual hosts to get rid of that annoying localhost/bla/public
Second of all things, you try to access this route: yourdomain/spanel/test/index, but there is no indication of you assigning the part /spanel anywhere in your route, all you assign is /test[...]
So in short, you want to access yourdomain/test/index or you want to modify your route to inclide the /spanel part

ZF2 run the application without using the router

I am learning ZF2.
Is it possible to run the application without Router as in Zf1?
Do we need to define router for each controller?
Example:
In ZF1: "admin/index/index" shows as "module/controller/action"
IN ZF2: "admin/index/index" shows as "router[/:controller][/:action]"
please help me to clear my doubts.
Please tyr this
return array(
// routes
'router' => array(
'routes' => array(
'album' => array(
'type' => 'Literal',
'options' => array(
'route' => '/album',
'defaults' => array(
'controller' => 'album\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(
// add the default namespace for :controllers in this route
'__NAMESPACE__' => 'album\Controller',
),
),
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'album\Controller\Test' => 'Album\Controller\TestController',
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
You need to add your controller name in invokables manually or invoke via Abstract Factories
'controllers' => array(
'invokables' => array(
'album\Controller\Test' => 'Album\Controller\TestController',
),
),
Automatic Controller Invokables via Abstract Factories
Reference

Categories