我正在尝试使用zend 2框架。
我的问题是我不太了解zend的路由是如何工作的。 我的问题是我不知道如何在路由中添加第二个控制器。
有人可以举个例子吗?
这是我的module.config.php:
<?php
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Website\Controller\Home',
'action' => 'index',
),
),
),
'website' => array(
'type' => 'Literal',
'options' => array(
'route' => '/website',
'defaults' => array(
'__NAMESPACE__' => 'Website\Controller',
'controller' => 'Home',
'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(
),
),
),
),
),
),
),
'navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'home',
),
array(
'label' => 'Website',
'route' => 'website',
'pages' => array(
array(
'label' => 'Home',
'route' => 'home',
'action' => 'home',
),
),
),
),
),
'service_manager' => array(
'factories' => array(
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
),
),
'controllers' => array(
'invokables' => array(
'Website\Controller\Home' => 'Website\Controller\HomeController',
'Website\Controller\Leden' => 'Website\Controller\LedenController'
),
),
'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/layout/layout.phtml',
'website/index/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(
__DIR__ . '/../view',
),
),
);
答案 0 :(得分:2)
请在您的问题上使用“修改”以了解有关您的问题的新增内容,因为所有关于您的问题的新通知的答案都是不舒服。 。。
与您的回购我发现下一个问题:
在您的网站/ config / module.config.php中 - 控制器 - &gt; invokables :尝试使用键'Leden' => 'Website\Controller\LedenController'
之后所有人都在工作。
我认为您的网站模块刚从骨架重命名为应用程序..
无论如何ZF2的最佳变体 - 模块化结构(教程:Album module, application.config.php,Album module :: step by step)
问题补丁:
module/Website/config/module.config.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/module/Website/config/module.config.php b/module/Website/config/module.config.php
index b3ff158..c128587 100644
--- a/module/Website/config/module.config.php
+++ b/module/Website/config/module.config.php
@@ -18,7 +18,7 @@ return array(
'route' => '/website',
'defaults' => array(
'__NAMESPACE__' => 'Website\Controller',
- 'controller' => 'Home',
+ 'controller' => 'Website\Controller\Home',
'action' => 'index',
),
),
@@ -70,7 +70,7 @@ return array(
'controllers' => array(
'invokables' => array(
'Website\Controller\Home' => 'Website\Controller\HomeController',
- 'Website\Controller\Leden' => 'Website\Controller\LedenController'
+ 'Leden' => 'Website\Controller\LedenController'
),
),
'view_manager' => array(
答案 1 :(得分:1)
你的module.config.php类似于skeleton application
中的示例路由器对吧?
因此,默认情况下,路由配置为/<module_name>/:controller/:action
因为ZF2使用模块系统来组织每个模块中的应用程序 - 请参阅Modules
例如 - 如果你想添加一个新的控制器(我在你的配置中看到 - Home&amp; Leden Controller):
├───module │ ├───Website ... │ │ │ │ │ ├───src │ │ │ └───Website │ │ │ ├───Controller │ │ │ │ HomeController.php │ │ │ │ LedenController.php
您的控制器示例:
namespace Website\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class HomeController extends AbstractActionController
{
public function writeAction()
{
...
}
...
}
路线:
'Website' => array(
'type' => 'Literal',
'options' => array(
'route' => '/website',
'defaults' => array(
'__NAMESPACE__' => 'Website\Controller',
'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(
),
),
),
),
),
和你的invokables:
'controllers' => array(
'invokables' => array(
'Website\Controller\Home' => 'Website\Controller\HomeController',
'Website\Controller\Leden' => 'Website\Controller\LedenController'
),
),
全部,尝试打开
有关ZF2中关于路由的详细信息,请参阅Zend\Mvc - Routing并参阅教程中的Routing and controllers