我是zend Framework2的新手,我试图将相册模块添加到ZF2的skelton应用程序中,但出现了404错误,Page not found。请求的URL无法通过路由匹配。我的相册/config/module.config.php代码是
<?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',
),
),
),
),
),
);在Application/config/module.config.php中,我添加了以下几行:
'modules' => array(
'Application',
'Album'
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
'module_paths' => array(
'./module',
'./vendor',
),
),有没有人能帮我改正代码?
发布于 2013-09-30 13:56:08
问题出在您的路由配置中:
'defaults => array(
'__NAMESPACE__' => 'Album\Controller',
'controller' => 'Album\Controller\Album',
)这样,您就可以告诉路由器装入以下类
Album\Controller\Album\Controller\Album__NAMESPACE__将优先于您指定为controller的任何内容。所以你有两个选择:
跳过__NAMESPACE__
controller
虽然这完全由您决定,但就我个人而言,我选择跳过__NAMESPACE__,因为最终我们所做的一切都是使用键,根据我对事物的理解,键不是类,因此不应该有名称空间:D
发布于 2013-10-05 18:05:58
你需要在你的代码中做一些dude...nothing错误的小改动。
NameSpace
如果指定Namespace,则路由中只需包含控制器名称:
'defaults' => array(
'__NAMESPACE__' => 'Album\Controller',
'controller' => 'Album' /* Not like this: 'Album\Controller\Album' */
'action' => 'index',
),如果不包含命名空间,则返回
'defaults' => array(
'controller' => 'Album\Controller\Album' /* Include this */
'action' => 'index',
),发布于 2014-04-22 21:57:56
您应该使用类似于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(
),
),
),
),
),
),
),您只需添加以下代码:
'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',
),
),
),将此代码添加到'child-routes‘键,然后您将访问url: localhost/module/:controller/:id。现在它成功了!
https://stackoverflow.com/questions/19086797
复制相似问题