Search code examples
phpzend-frameworkzend-framework2

Zend Framework 2, "resolves to invalid controller class or alias" Blog module from the tutorial


i have been following the steps of the Blog tutorial from Zend mainpage(https://framework.zend.com/manual/2.4/en/in-depth-guide/first-module.html) but i got stuck on this error: "resolves to invalid controller class or alias".

heres my 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 */ )
 );

heres my 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';
}
}

my ListController.php:

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

use Zend\Mvc\Controller\AbstractActionController;

class ListController extends AbstractActionController
{
}

it seems fully equal to the guide code, but i might have miss something. The blog module was declared on the application config too. Help is apreciated :s thank you for your time


Solution

  • Problem is in your module.config.php file. You have there 3 times return statement, so only first part (router configuration) of configuration file is returned, rest is skipped.

    Your configuration file should looks like this:

    // 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',
            ),
        )
    );