I'me developing an app in zf2. when I define routes in module.config.php and access them in my browser, php throws error :
Fatal error: Class 'BookList\src\BookList\Controller\BookController' not found in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\projects\autoclick\skeleton-application\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php on line 170
here is my module.config.php :
return array(
'controllers' => array(
'invokables' => array(
'BookList\src\BookList\Controller\Book' => 'BookList\src\BookList\Controller\BookController'
)
),
'router' => array(
'routes' => array(
'book' => array(
'type' => 'segment',
'options' => array(
'route' => '/book[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+'
),
'defaults' => array(
'controller' => 'BookList\src\BookList\Controller\Book',
'action' => 'index'
)
)
)
)
),
'view_manager' => array(
'template_path_stack' => array(
'book' => __DIR__ . '/../view'
)
)
I have my BookController namespaced BookList\src\BookList\Controller
This should be:
'controllers' => array(
'invokables' => array(
'BookList\Controller\Book' => 'BookList\Controller\BookController' // <- change key and value
)
),
'router' => array(
'routes' => array(
'book' => array(
'type' => 'segment',
'options' => array(
'route' => '/book[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+'
),
'defaults' => array(
'controller' => 'BookList\Controller\Book', // <- change
'action' => 'index'
)
)
)
)
),
You should not specify the path, but only the class name, e.g. remove \BookList\src\ part. The namespace also should not container the src folder
Related
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 ... {
}
my config value are
'controllers' => array(
'invokables' => array(
'System\Controller\Index' => 'System\Controller\IndexController',
'System\Controller\Config' => 'System\Controller\ConfigController'
),
),
'view_manager' => array(
'template_path_stack' => array(
'system' => __DIR__ . '/../view',
)
),
'router' => array(
'routes' => array(
// using the path /application/:controller/:action
'system' => array(
'type' => 'segment',
'options' => array(
'route' => '/system/index[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'System\Controller\Index',
'action' => 'index',
),
),
),
),
),
and directory stutrue are in pics.
Is there any things else I am missing ? because i am getting error msg in zf2 application .
Error Msg :
Zend\View\Renderer\PhpRenderer::render: Unable to render template "system/index/index"; resolver could not resolve to a file
Try this:
'view_manager' => array(
'template_path_stack' => array(
'system' => array(__DIR__ . '/../view'),
)
),
I added a reseller subdomain on my myhost.com (reseller.myhost.com) and I use it for routing to my Reseller module. Read this question I posted before here: click here
My Reseller route config looks this:
'router' => array(
'routes' => array(
'Reseller' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'reseller.myhost.com',
'constraints' => array(
),
'defaults' => array(
'controller' => 'Reseller\Controller\Reseller',
'action' => 'index'
)
),
'may_terminate' => true,
'child_routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'__NAMESPACE__' => 'Reseller\Controller',
'controller' => 'Reseller',
'action' => 'index',
),
),
),
)
)
)
)
My createdAd route matches on reseller.myhost.com/createdAd but I expect routes from other modules not work on this reseller subdomain.
and here is my advertise route configuration
'router' => array(
'routes' => array(
'locate' => array(
'type' => 'segment',
'options' => array(
'route' => '/locate[/:cityName][/:CityId][/:CategoryId][/:categoryName]',
'constraints' => array(
),
'defaults' => array(
'controller' => 'Advertise\Controller\Advertise',
'action' => 'index',
),
),
),
'createAd' => array(
'type' => 'segment',
'options' => array(
'route' => '/createAd[/:subCategoryId]',
'constraints' => array(
),
'defaults' => array(
'controller' => 'Advertise\Controller\Advertise',
'action' => 'createAd',
),
),
),
),
),
));
be notice that i want to advertise module work without subdomain and work normally and only reseller module work with subdomain
Why does this occur?
I understand from your question: you expect the createAd route not to work on the subdomain. So reseller.myhost.com/createdAd should not match instead you want a to match on the route without subdomain myhost.com/createdAd.
I would suggest that you should create a separate route definition for the Advertise module.
Your route config in Advertise module (module/Advertise/config/module.config.php)
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Advertise\Controller\Advertise',
'action' => 'index'
)
),
)
'createAd' => array(
'type' => 'Literal',
'options' => array(
'route' => '/createAd',
'defaults' => array(
'controller' => 'Advertise\Controller\Advertise',
'action' => 'createAd',
)
)
)
)
)
Your route config in Reseller module (module/Reseller/config/module.config.php)
'router' => array(
'routes' => array(
'Reseller' => array(
'type' => 'Hostname',
'options' => array(
'route' => ':reseller.myhost.com',
),
'may_terminate' => false,
'child_routes' => array(
'home' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Reseller\Controller\Reseller',
'action' => 'index'
)
)
)
)
)
)
),
You can distinguish matches because of the subdomain.
The routes home and createAdd match the Advertise module without subdomain.
The route reseller.home matches the index in Reseller module within subdomain reseller.myhost.com.
Check for more details also the Hostname routing example here in the ZF2 documentation
You should have a "root" hostname for all your standard routes not on the subdomain route. Eg:
'router' => array(
'routes' => array(
'myhost' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'myhost.com',
),
),
),
),
Now you can add your 'createAd' route (and other routes) as a child route of the 'myhost' route. Eg:
'router' => [
'routes' => [
'myhost' => [
'child_routes' => [
'createAd' => array(
'type' => 'segment',
'options' => array(
'route' => '/createAd[/:subCategoryId]',
'constraints' => array(
),
'defaults' => array(
'controller' => 'Advertise\Controller\Advertise',
'action' => 'createAd',
),
),
),
],
],
],
],
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