我目前在ZF2工作,需要一些帮助。 不幸的是,我只能找到示例设置只有一个模块的情况的路由。或者示例中仍然包含应用程序模块,具有动态段路径。我想完全删除应用程序模块,只有我自己的模块运行配置所有路由。
我有两个模块: CLFrontend, CLBackend
我的应用程序配置如下所示:
return array(
'modules' => array(
'ClFrontend',
'ClBackend'
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
'module_paths' => array(
'./module',
'./vendor',
),
),
);
我想在那里注册我自己的两个模块。现在路由应该看起来像这样:
> /下面的所有内容都应该转到前端模块摘录/后端/ - > IndexController - >的indexAction
/ controller1 - > Controller1Controller - >的indexAction
/ controller1 / add - > Controller1Controller - >的addAction
/ controller1 / add / 1 / - > COntroller1Controller - > addaction - >第1项
现在,/ backend下的所有内容都应该路由到后端模块
/ backend - > BackendIndexController - >的indexAction
/ backend / controller1 - > BackendController1Controller - >的indexAction
/ backend / controller1 / add - > BackendController1Controller - > 的addAction
/ backend / controller1 / add / 1 / - > BackendCOntroller1Controller - > addaction - >第1项
我想定义固定的路线,而不是类似于路段的路线:
:模块/:控制器/:动作
我希望最终得到像
这样的东西/
/控制器1 / [:操作[/:ID]]
和
/后端
/后端/ backendcontroller / [:操作[/:ID]]
Myapproach如下。问题是,即使后端路由似乎与前端模块匹配?!我要么得到一个404
路由无法匹配请求的网址。
或
致命错误:找不到类'ClBackend \ Controller \ AnswerController' 在 / / 的 * / ** / * 的** / checklistenassistent3 /供应商/ ZF2 /库/ Zend的/的ServiceManager / AbstractPluginManager.php 在第177行
CLFrontend /配置/ module.config.php
return array(
'controllers' => array(
'invokables' => array(
'ClFrontend\Controller\Index' => 'ClFrontend\Controller\IndexController',
'ClFrontend\Controller\User' => 'ClFrontend\Controller\UserController',
),
),
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'ClFrontend\Controller\Index',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/cl-frontend/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/cl-frontend/index/index.phtml',
'error/404' => __DIR__ . '/../view/cl-frontend/error/404.phtml',
'error/index' => __DIR__ . '/../view/cl-frontend/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
CLBackend /配置/ module.config.php
return array(
'controllers' => array(
'invokables' => array(
'ClBackend\Controller\Answer' => 'ClBackend\Controller\AnswerController',
'ClBackend\Controller\AnswerGroup' => 'ClBackend\Controller\AnswerGroupController',
'ClBackend\Controller\Category' => 'ClBackend\Controller\CategoryController',
'ClBackend\Controller\Checklist' => 'ClBackend\Controller\ChecklistController',
'ClBackend\Controller\Index' => 'ClBackend\Controller\IndexController',
'ClBackend\Controller\Question' => 'ClBackend\Controller\QuestionController',
'ClBackend\Controller\User' => 'ClBackend\Controller\UserController',
),
),
'router' => array(
'routes' => array(
'backend' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/backend',
'defaults' => array(
'controller' => 'ClBackend\Controller\Index',
'action' => 'index',
),
'may_terminate' => true
),
'child_routes' => array (
'answer' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/answer/:action/:id',
'defaults' => array(
'controller' => 'ClBackend\Controller\Answer',
'action' => 'index',
),
),
),
'answergroup' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/answergroup/:action/:id',
'defaults' => array(
'controller' => 'ClBackend\Controller\AnswerGroup',
'action' => 'index',
),
),
),
'category' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/category/:action/:id',
'defaults' => array(
'controller' => 'ClBackend\Controller\Category',
'action' => 'index',
),
),
),
'checklist' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/checklist/:action/:id',
'defaults' => array(
'controller' => 'ClBackend\Controller\Checklist',
'action' => 'index',
),
),
),
'question' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/question/:action/:id',
'defaults' => array(
'controller' => 'ClBackend\Controller\Question',
'action' => 'index',
),
),
),
'user' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/user/:action[/:id]',
'defaults' => array(
'controller' => 'ClBackend\Controller\User',
'action' => 'index',
),
),
),
),
),
),
),
);
答案 0 :(得分:0)
您是否考虑过控制器工厂?它允许您匹配单个路由并使用逻辑来决定使用哪个控制器。
例如,您的路线可能如下:
'backend-default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:controller_type[/:action][/id]',
'defaults' => array(
'action' => 'index',
'controller' => 'MyFactory'
),
),
),
如果此路线匹配,则将使用控制器工厂(MyFactory) - 在此工厂中,您可以访问路线匹配参数。使用这些参数,您应该能够返回适当的控制器。
你甚至可以传递一个额外的参数来表示它是一个后端控制器(并使用一个工厂)。
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class MyFactoryController implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
// $serviceLocator is a Zend\Mvc\Controller\ControllerManager
$app = $serviceLocator->getServiceLocator()->get('application');
$routeMatch = $app->getMvcEvent()->getRouteMatch();
var_dump($routeMatch);
/**
* Create controller based off $routeMatch params
*/
return $controller;
}
}
控制器工厂允许您将controller_type变量查找为有效的类名 - 或者将可调用的名称前缀为controller_type。
我不确定自己会采取这条路线,但我希望这对你想要实现的目标有所帮助。