【问题标题】:Route with name "course" not found, zend framework 2找不到名称为“课程”的路线,zend框架2
【发布时间】:2019-02-13 22:02:21
【问题描述】:

我无法解决这个问题...我是 zend 的新手,这是来自 zend 的修改示例

Zend\Mvc\Router\Exception\RuntimeException 文件:C:\wamp\www\test\vendor\zendframework\zendframework\library\Zend\Mvc\Router\Http\TreeRouteStack.php:317 信息: 找不到名称为“课程”的路线

这是我的 module.config.php

<?php 

return array(
 'controllers' => array(
      'invokables' => array(
         'Course\Controller\Index' => 'Course\Controller\IndexController',
        ),  ),
    'router' => array(
         'routes' => array(
           'index' => array(
                 'type' => 'Literal',
                     'options' => array(
                     'route' => '/course',
                     'defaults' => array(
                         'controller' => 'Course\Controller\Index',
                         'action'     => 'index'
                     ),
                 ),
             ),
         )
     ),
                'view_manager' => array(
     'template_path_stack' => array(
         'course' => __DIR__ . '/../view/'
     ),
     )


 );

和 IndexController.php

    <?php 

namespace Course\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController{   

    protected $courseTable;


public function indexAction()
    {
     return new ViewModel(array(
             'courses' => $this->getCourseTable()->fetchAll(),
         ));
    }

    public function getCourseTable()
     {
         if (!$this->courseTable) {
             $sm = $this->getServiceLocator();
             $this->courseTable = $sm->get('Course\Model\CourseTable');
         }
         return $this->courseTable;
     }
}

请大家帮帮我,因为我不知道这有什么问题。

【问题讨论】:

    标签: zend-framework2


    【解决方案1】:

    您的配置定义了一个路由,称为 index。如果你希望它被称为“课程”,你需要重命名它。我已经在您的配置中突出显示了路由名称:

    'router' => array(
         'routes' => array(
           'index' => array( // <-- this array key is the route name
                 'type' => 'Literal',
                     'options' => array(
                     'route' => '/course',
                     'defaults' => array(
                         'controller' => 'Course\Controller\Index',
                         'action'     => 'index'
                     ),
                 ),
             ),
         )
     ),
    

    【讨论】:

    • 但是当我重命名它时,我看到这个错误:致命错误:Zend\Mvc\Router\Exception\RuntimeException: Route with name "index" not found in C:\wamp\www\test \vendor\zendframework\zendframework\library\Zend\View\Helper\Navigation\AbstractHelper.php 在第 169 行,所以如果你能告诉我更多关于它的信息,我将不胜感激
    • 听起来您在视图中用两个不同的名称来指代路线。你需要保持一致。
    【解决方案2】:

    我的问题是我没有在 /config/modules.config.php 中注册新创建的模块

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 2012-09-18
      相关资源
      最近更新 更多