i'm new to zend framework 2 and was trying to add the album module into ZF2's skelton application but getting A 404 error occurred Page not found. The requested URL could not be matched by routing. my Album/config/module.config.php code is
<?php
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view'
),
),
'router' => array(
'routes' => array(
'album' => array(
//'type' => 'segment',
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
//'route' => '/album[/][:action][/:id]',
//'route' => '/:controller[.:formatter][/:id]',
'route' => '/album',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'formatter' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'Album\Controller',
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
);
and in Application/config/module.config.php i have added these lines:
'modules' => array(
'Application',
'Album'
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
'module_paths' => array(
'./module',
'./vendor',
),
),
can anyone plz help me to correct the codes...
The Problem is within your Route-Configuration:
'defaults => array(
'__NAMESPACE__' => 'Album\Controller',
'controller' => 'Album\Controller\Album',
)
With this you tell the Router to load the following class
Album\Controller\Album\Controller\Album
The __NAMESPACE__
will be prepended to whatever you assign as controller
. So you have two options:
__NAMESPACE__
controller
While this is completely up to you, personally I choose to skip the __NAMESPACE__
since ultimately all we're doing is working with keys and with the way i understand things, keys are no classes and therefore shouldn't have a namespace :D
You need little changes dude...nothing wrong with your code.
NameSpace
If You specify Namespace, you have to include only the controller name in routing:
'defaults' => array(
'__NAMESPACE__' => 'Album\Controller',
'controller' => 'Album' /* Not like this: 'Album\Controller\Album' */
'action' => 'index',
),
If no namespace included
'defaults' => array(
'controller' => 'Album\Controller\Album' /* Include this */
'action' => 'index',
),
You should use configuration like Application module in ZendSkeletonApplication:
'router' => array(
'routes' => array(
'album' => array(
'type' => 'Literal',
'options' => array(
'route' => '/album',
'defaults' => array(
'__NAMESPACE__' => 'Album\Controller',
'controller' => 'Album',
'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(
),
),
),
),
),
),
),
You just add the following code:
'album' => array(
'type' => 'Segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
),
'defaults' => array(
'__NAMESPACE__' => 'Album\Controller',
'controller' => 'Album',
'action' => 'index',
),
),
),
add this code to 'child-routes' key and after that you'll access to url: localhost/module[/:controller][/:action][/:id]. And now it's worked!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.