I am looking for some help with the ZF2 router configuration.
I would like to add multiple controllers to a single module in a development context, although will eventually have to figure out how to specify specific routes.
To do this I am currently trying to use the general purpose module.config specified in the MVC quick start documentation. However this does not seem to perform as the ZF2 documentation would suggest.
http://framework.zend.com/manual/2.0/en/modules/zend.mvc.quick-start.html
The documentation notes:
"ZendSkeletonApplication ships with a “default route” that will likely get you to this action. That route basically expects “/{module}/{controller}/{action}”, which allows you to specify this: “/zend-user/hello/world”. We’re going to create a route here mainly for illustration purposes, as creating explicit routes is a recommended practice."
However, the controller renders /landlord, /landlord/home/index, but not /landlord/sandbox/index. Home and sandbox are controllers. Sandbox has been named in line with the naming convention "SandboxController". My take is that perhaps the child_routes section of the code in the documentation needs some sort of modification that I have missed.
In the sandbox instance I get this error on the 404 error page.
Landlord\Controller\Sandbox(resolves to invalid controller class or alias: Landlord\Controller\Sandbox)
I tried adding 'Landlord\Controller\Sandbox' => 'Landlord\Controller\SandboxController', to the invokables, but this creates an error.
My controller is listed below:
return array(
'controllers' => array(
'invokables' => array(
'Landlord\Controller\Home' => 'Landlord\Controller\HomeController',
),
),
'router' => array(
'routes' => array(
'landlord' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/landlord',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Landlord\Controller',
'controller' => 'home',
'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.
'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(
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'landlord' => __DIR__ . '/../view',
),
),
);
If there is a simple way to directly configure urls, this would also be useful, or a really good tutorial this would also be appreciated.
The problem here is that even though the router would try to route to your new controller that you added you still need to add the Invokable part for every single controller that you add. So your router appears to be fine at this point but if you add for instance a new AboutController then you will need to modify the top of your config like so...
return array(
'controllers' => array(
'invokables' => array(
'Landlord\Controller\Home' => 'Landlord\Controller\HomeController',
'Landlord\Controller\About' => 'Landlord\Controller\AboutController',
),
),
Just find the answer... posting here, it may help some one... I assume that you have follow the example of album module provided by zf2 docs, and now adding new modules, like in my case 'Admin' and i add new controllers like admin,news etc,
Here is the routers settings...
'router' => array(
'routes' => array(
'admin' => array(
'type' => 'segment',
'options' => array(
'route' => '/admin/admin[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Admin\Controller\Admin',
'action' => 'index',
),
),
),
'news' => array(
'type' => 'segment',
'options' => array(
'route' => '/admin/news[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Admin\Controller\News',
'action' => 'index',
),
),
),
),
),
And here add your controller..
'controllers' => array(
'invokables' => array(
'Admin\Controller\News' => 'Admin\Controller\NewsController',
'Admin\Controller\Admin' => 'Admin\Controller\AdminController',
),
),
Now you can call your controllers with in your module , like here in my case
localhost/{foldername}/admin/news/index and localhost/{foldername}/admin/admin/index
Thanks
I was in the same case, I found a solution as the previous post, but I think it's not the better way.
In order to add le "clientController" inside my routeur:
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Fcm\Controller\Index',
'action' => 'index',
),
),
),
'client' => array(
'type' => 'Segment',
'options' => array(
'route' => '/client[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Fcm\Controller\Client',
'action' => 'index',
),
),
),
),
),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With