Ale*_*ini 10 php zend-framework2
我的模块路由有问题.我有2个模块,应用程序和管理员.每个模块都将indexAction作为默认操作:
localhost/ - >应用程序/索引
localhost/admin/ - >管理员/索引
管理员/索引仅适用于localhost/admin/index /
当模块名称以字母"A"开头时,会发生此问题.如果我将管理员重命名为"汽车",localhost/cars /正常工作!
错误是:
A 404 error occurred
The requested controller was unable to dispatch the request.
Controller:
Application\Controller\Application
No Exception available
Run Code Online (Sandbox Code Playgroud)
这是Application模块中的module.config.php:
<?php
return array(
'router' => array(
'routes' => array(
'Application' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/][:action/]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\Application',
'action' => 'index',
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Application' => 'Application\Controller\IndexController'
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'Application/Application/index' => __DIR__ . '/../view/Application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
'Application' => __DIR__ . '/../view',
),
),
);
?>
Run Code Online (Sandbox Code Playgroud)
这是Admin模块中的module.config.php:
<?php
return array(
'router' => array(
'routes' => array(
'Admin' => array(
'type' => 'Segment',
'options' => array(
'route' => '/admin/[:action/]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
'controller' => 'Admin\Controller\AdminController',
'action' => 'index'
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'Admin\Controller\AdminController' => 'Admin\Controller\AdminController'
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'template_path_stack' => array(
'Admin' => __DIR__ . '/../view',
),
),
);
?>
Run Code Online (Sandbox Code Playgroud)
IndexController.php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction(){
}
}
Run Code Online (Sandbox Code Playgroud)
AdminController.php
namespace Admin\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AdminController extends AbstractActionController
{
public function indexAction()
{}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?
The requested controller was unable to dispatch the request.首先,仅当路由器无法将请求分派到其定义的操作时,才会发生错误。因此,请验证您的控制器是否正确,并且操作是否存在且可调用。
正如 allready 指出的那样,您的/admin/路线将指向两个配置的端点。当管理路由在配置中的应用程序路由之前定义时,这首先不是问题。
因此,您的路线/admin/永远不会路由到您的路线AdminController,因为其他动态路线将首先匹配。
为了获得预期的结果,请使用priority路线中的设置,以确保您的路线将在您的路线Admin之前匹配。Application
'router' => array(
'routes' => array(
'Admin' => array(
'priority' => 100,
'type' => 'Segment',
'options' => array(
'route' => '/admin/[:action/]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
'controller' => 'Admin\Controller\AdminController',
'action' => 'index'
),
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
除了你的问题之外,不要以 结束 php 脚本,?>因为当结束标记后面有空格时,这可能会导致错误。