Zend Framework 2,"解析为无效的控制器类或别名"教程中的博客模块

时间:2017-03-06 11:04:23

标签: php zend-framework zend-framework2

我一直在关注Zend主页(https://framework.zend.com/manual/2.4/en/in-depth-guide/first-module.html)的博客教程的步骤,但我遇到了这个错误:"解析为无效的控制器类或别名"。

继承我的module.config.php:

<?php
// Filename: /module/Blog/config/module.config.php
return array(
// This lines opens the configuration for the RouteManager
 'router' => array(
     // Open configuration for all possible routes
     'routes' => array(
         // Define a new route called "post"
         'post' => array(
             'type' => 'literal',
             // Configure the route itself
             'options' => array(
                 // Listen to "/blog" as uri
                 'route'    => '/blog',
                 'defaults' => array(
                     'controller' => 'Blog\Controller\List',
                     'action'     => 'index',
                 )
              )
           )
         )
        )
      );
 return array(
 'controllers' => array(
     'invokables' => array(
         'Blog\Controller\List' => 'Blog\Controller\ListController'
     )
 ),
 'router' => array( /** Route Configuration */ )
 );


 return array(
     'view_manager' => array(
       'template_path_stack' => array(
         __DIR__ . '/../view',
     ),
 ),
 'controllers' => array( /** Controller Configuration */),
 'router'      => array( /** Route Configuration */ )
 );

继承我的Module.php:

<?php
// Filename: /module/Blog/Module.php
namespace Blog;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module implements
AutoloaderProviderInterface,
ConfigProviderInterface
{
/**
    * Return an array for passing to Zend\Loader\AutoloaderFactory.
    *
    * @return array
*/
public function getAutoloaderConfig()
{
    return array(
    'Zend\Loader\StandardAutoloader' => array(
        'namespaces' => array(
            __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
         )
     )
     );
}

/**
* Returns configuration to merge with application configuration
*
* @return array|\Traversable
*/
public function getConfig()
{
    return include __DIR__ . '/config/module.config.php';
}
}

我的ListController.php:

<?php
// Filename: /module/Blog/src/Blog/Controller/ListController.php
namespace Blog\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class ListController extends AbstractActionController
{
}

它似乎完全等于指南代码,但我可能会错过一些东西。博客模块也在应用程序配置中声明。 帮助是赞成的:谢谢你的时间

1 个答案:

答案 0 :(得分:4)

问题出在您的module.config.php文件中。你有3次return语句,所以只返回配置文件的第一部分(路由器配置),跳过休息。

您的配置文件应如下所示:

// Filename: /module/Blog/config/module.config.php
return array(
// This lines opens the configuration for the RouteManager
    'router' => array(
        // Open configuration for all possible routes
        'routes' => array(
            // Define a new route called "post"
            'post' => array(
                'type' => 'literal',
                // Configure the route itself
                'options' => array(
                    // Listen to "/blog" as uri
                    'route'    => '/blog',
                    'defaults' => array(
                        'controller' => 'Blog\Controller\List',
                        'action'     => 'index',
                    )
                )
            )
        )
    ),
    'controllers' => array(
        'invokables' => array(
            'Blog\Controller\List' => 'Blog\Controller\ListController'
        )
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    )
);