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 ... {
}
Related
I have a SkeletonApplication installed and implemented some controllers into the standard module 'Application'.
That works fine.
But now I want to use a second module and I want to set a route from within the 'Application'-module to the new module for linking it there in a view.
The Second module is named 'Sporttabs'.
In my application.config.php I've set as found in documentation:
// This should be an array of module namespaces used in the application.
'modules' => array(
'Application',
'Sporttabs'
),
In the module 'Application' I set in module.config.php:
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'module' => 'Application',
'controller' => 'Index',
'action' => 'index',
),
),
),
'fach' => array(
'type' => 'segment',
'options' => array(
'route' => '/index[/: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(
'controller' => 'Index',
'action' => 'index'
),
),
),
'sporttabs' => array(
'type' => 'segment',
'options' => array(
'route' => '/sporttabs[/: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(
'module' => 'Sporttabs',
'controller' => 'Sporttab',
'action' => 'index'
),
),
),
),
),
I tried linking it within index.phtml:
Sporttabs-Projekt
This doesn't work, I only get /sporttab
Even if I try to do www.myurl.de/sporttabs I don't get into the Sporttabs-Module...
(I'm using ZendStudio to generate the ne Module, so I think all file are in right position...)
Can you give me a hint how to do this ?
There is no need to define sporttabs inside your Application config. I suggest you do this inside your Sporttabs' module.config.php file.
This is an example of my /admin route, which is a different module named Admin, which sit next to Application.
'router' => [
'routes' => [
'admin' => [
'type' => 'Literal',
'options' => [
'route' => '/admin',
'defaults' => [
'__NAMESPACE__' => 'Admin\Controller',
'controller' => 'Index',
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'default' => [
'type' => 'Segment',
'options' => [
'route' => '/[:controller[/][:action[/][:id][/page/:page][/search/:search]]]',
'constraints' => [
'controller' => '[a-zA-Z0-9_-]*',
'action' => '[a-zA-Z0-9_-]*',
'search' => '[a-zA-Z0-9_-]*',
'id' => '[0-9]+',
'page' => '[0-9]+',
],
'defaults' => [
'__NAMESPACE__' => 'Admin\Controller',
'controller' => 'Index',
'action' => 'index',
],
],
],
],
],
],
],
From there i do this:
<?=$this->url("admin/default", ['controller' => "controler_name", "action" => "action_name_from_controller", "id" => id_or_something_else_if_needed])?>
/default is there in order to have access to child routes.
#Stanimir hits the right hint for my solution:
While editing the routing for my application, I must have made a unintentional change in the order of the routes array:
The 'may_terminate' and 'child_routes' section moved to a upper level instead of being part of the 'fach'-route.
So I changed the array as follows:
'routes'=> array(
'fach'=> array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'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]+',
),
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
),
),
),),
As an aftereffect of including the Namespace in the routing, I also had to change the controllers-array, because the alias name of the controller changed from 'Index' to 'Application\Controller\Index':
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
),
),
The same error blocked me from getting into the second module, the routes-array was misordered too.
Now the link-solution Stanimir posted in his answer works fine an I get into my new module...
Thanks for your help Stanimir!
I'm trying to do some routing in Zend Framework 2, but it's not working.
The basics of the skeleton application are working, so I added a new module called User and the following code in the file \module\User\config\module.config.php
'controllers' => array(
'invokables' => array(
'User\Controller\User' => 'User\Controller\UserController',
),
),
'router' => array(
'routes' => array(
'login' => array(
'type' => 'Literal',
'options' => array(
'route' => '/login',
'defaults' => array(
'__NAMESPACE__' => 'User\Controller',
'controller' => 'User',
'action' => 'login',
),
),
),
'user_create' => array(
'type' => 'Literal',
'options' => array(
'route' => '/user/create',
'defaults' => array(
'__NAMESPACE__' => 'User\Controller',
'controller' => 'User',
'action' => 'create',
),
),
),
),
),
If I try to access the first route (/login), it works.
But the second route (/user/create) results in the error:
File:
F:\www\ZendVendas\library\Zend\Mvc\Router\Http\TreeRouteStack.php:313
Message:
Route with name "create" not found
If I do create a route without the controller name, it works:
'create' => array(
'type' => 'Literal',
'options' => array(
'route' => '/create',
'defaults' => array(
'__NAMESPACE__' => 'User\Controller',
'controller' => 'User',
'action' => 'create',
),
),
),
But I would want the route were "/user/create", and don't "/create".
I have searched for many topics, but can't see where is my mistake.
Appreciate any help ;)
Edit: ajusted code with suggestions of #Jurian
'router' => array(
'routes' => array(
'user' => array(
'type' => 'Literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'profile',
),
),
'child_routes' => array(
'login' => array(
'type' => 'Literal',
'options' => array(
'route' => '/login',
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'login',
),
),
),
'create' => array(
'type' => 'Literal',
'options' => array(
'route' => '/create',
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'create',
),
),
),
),
),
),
),
You have to understand how routing works in Zend Framework 2. Routes have a name and some configuration. The structure looks as follows:
'router' => array(
'routes' => array(
'route_name_1' => array( /* config here */ ),
'route_name_2' => array( /* config here */ ),
'route_name_3' => array( /* config here */ ),
),
),
Here the route names are route_name_1 etc. If you assemble an url, you use that route name. So if route_name_1 has the url /foo/bar/baz, you can ask for the url of route_name_1 by using the url view helper:
echo $this->url('route_name_1'); // prints /foo/bar/baz
Your url /user/create is mapped to the route name user_create so to assemble this url, you need to pass on the route name:
echo $this->url('user_create'); // prints /user/create
CHILD ROUTES
There is also a concept of child routes. This can give you a route user which maps to /user and then this user route has a child create which maps to /create and as such the "total" route of create is /user/create. This can be configured as follows:
'router' => array(
'routes' => array(
'route_name_1' => array( /* config here */ ),
'route_name_2' => array(
/* config here */
'child_routes' => array(
'child_name_1' => array( /* config here */ ),
'child_name_2' => array( /* config here */ ),
),
),
),
),
Now, if you want to assemble an url for route_name_2 it just looks as above:
echo $this->url('route_name_1');
But if you need to assemble the url for child_name_1 you construct a "path" with a / between the name and its parent(s):
echo $this->url('route_name_1/child_name_1');
So although you can access the /user/create route fine with the route name you already have, you might want to use child routes as this gives you a more flexible routing system:
'router' => array(
'routes' => array(
'user' => array(
'type' => 'Literal',
'options' => array(
'route' => '/user/create',
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'profile',
),
),
),
'child_routes' => array(
'login' => array(
'type' => 'Literal',
'options' => array(
'route' => '/login',
'defaults' => array(
'action' => 'login',
),
),
),
'create' => array(
'type' => 'Literal',
'options' => array(
'route' => '/create',
'defaults' => array(
'action' => 'create',
),
),
),
),
),
),
),
Then you have a route user which maps to a "profile". If you assemble user/create you go to /user/create and it uses the "createAction" from the user controller. The same hapens with user/login route.
I have found what I was doing wrong.
In one of my view files, there was a URL function pointing to the route /create.
It would be much helpful if Zend indicated the file with the invalid route, but, once I found the mistake, everything is working now.
'router' => array(
'routes' => array(
'login' => array(
'type' => 'Literal',
'options' => array(
'route' => '/login',
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'login',
),
),
),
'logout' => array(
'type' => 'Literal',
'options' => array(
'route' => '/logout',
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'logout',
),
),
),
'user' => array(
'type' => 'Literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'profile',
),
),
'child_routes' => array(
'create' => array(
'type' => 'Literal',
'options' => array(
'route' => '/create',
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'create',
),
),
),
'edit' => array(
'type' => 'Literal',
'options' => array(
'route' => '/edit',
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'edit',
),
),
),
),
),
),
),
Thank you for the help!
I am having problem while implementing Multiple routes as defined Below in my snippet
EDIT: i am getting this exception too
Additional information:
Zend\Mvc\Exception\InvalidControllerException
with Message
Controller of type Account\Controller\VoucherController is invalid; must implement Zend\Stdlib\DispatchableInterface
<?php
namespace Account;
return array(
'controllers' => array(
'invokables' => array(
'Account\Controller\Account' => 'Account\Controller\AccountController',
'Account\Controller\Voucher' => 'Account\Controller\VoucherController',
),
// --------- Doctrine Settings For the Module
'doctrine' => array(
'driver' => array(
'account_entities' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Account/Entity')
),
'orm_default' => array(
'drivers' => array(
'Account\Entity' => 'account_entities'
)
)
)
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'account' => array(
'type' => 'segment',
'options' => array(
'route' => '/account[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Account\Controller\Account',
'action' => 'index',
),
),
),
'voucher' => array(
'type' => 'segment',
'options' => array(
'route' => '/account/voucher[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Account\Controller\Voucher',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'account' => __DIR__ . '/../view',
),
),
),
);
Now the issue is i am getting a 404, when i try to access MyHost/account/Voucher
P.S: I already have A Controller under Account/Controller/Voucher and a view under Account/View/Voucher named as index.phtml now i dont know what am i missing here.
As Adnrew and Timdev comments above that there is something not right in your controller, you can check few basic things in your controller, that you have following code correct. specially the typos.
namespace Account\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class VoucherController extends AbstractActionController {
// you acctions
}
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
I have created new module called 'currency' and configured routes in module.config. It is working fine.
After that I have added new controller called CrateController for currency rates.And also created forms, models and view files.
But It is not routing correctly.
Error:
Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render template "currency/crate/index"; resolver could not resolve to a file....
Any clue to check this out will be helpful.
My module.config file as follows.
return array(
'controllers' => array(
'invokables' => array(
'Currency\Controller\Currency' => 'Currency\Controller\CurrencyController',
'Currency\Controller\Crate' => 'Currency\Controller\CrateController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'currency' => array(
'type' => 'segment',
'options' => array(
'route' => '/currency[/:action][/:currency_id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'currency_id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Currency\Controller\Currency',
'action' => 'index',
),
),
),
'crate' => array(
'type' => 'segment',
'options' => array(
'route' => '/crate[/:action][/:c_rate_id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'c_rate_id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Currency\Controller\Crate',
'action' => 'index',
),
),
),
),
),
Check for two things:
First: Is the template file present? ./module/Currency/view/currency/crate/index.phtml
Second: Check for the following entry inside ./Currency/config/module.config.php
'view_manager' => array(
'template_path_stack' => array(
'currency' => __DIR__ . '/../view',
)
),