Search code examples
phpviewzend-framework2action

Page not found creating a new action controller in Zend Framework 2


I've got a big problem because I can see index.html give it by IndexController, but if I create another action, for example, getdataAction() I have the next error:

enter image description here

I think that I have not to configure anything to do that, but these are my files:

module_config.php

namespace Application;

return array(
'doctrine' => array(
        'driver' => array(
                'application_entities' => array(
                        'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                        'cache' => 'array',
                        'paths' => array(__DIR__ . '/../src/Application/Entity')
                ),

                'orm_default' => array(
                        'drivers' => array(
                                'Application\Entity' => 'application_entities'
                        )
                )
        )
),      
'router' => array(
    'routes' => array(
          'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action'     => 'index'
                ),
            ),
        ),
        // The following is a route to simplify getting started creating
        // new controllers and actions without needing to create a new
        // module. Simply drop new controllers in, and you can access them
        // using the path /application/:controller/:action
        'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/Application',
                'defaults' => array(
                    __NAMESPACE__ => 'Application\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(
                        ),
                    ),
                ),
            ),
        ),
    ),
),
'service_manager' => array(
    'abstract_factories' => array(
        'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
        'Zend\Log\LoggerAbstractServiceFactory',
    ),
    'factories' => array(
        'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
    ),
),
'translator' => array(
    'locale' => 'en_US',
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),
'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => Controller\IndexController::class
        //'Application\Controller\Index' => 'Application\Controller\IndexController'
    ),
),
'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',
        'application/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',
    ),
),
// Placeholder for console routes
'console' => array(
    'router' => array(
        'routes' => array(
        ),
    ),
),
);

IndexController.php

<?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Entity\UserEntity;

class IndexController extends AbstractActionController
{
protected $_objectManager;

public function indexAction(){
    try{    
        $users = $this->getObjectManager()->getRepository('\Application\Entity\UserEntity')->findAll();

        foreach ($users as $user){
            echo "[id]: " . $user->getId() . " Nombre: " . $user->getFullName() . "<br />";
        }
    }catch(Exception $ex){
        echo "error: " . $ex->getMessage();
    }   
    return new ViewModel(array("users"  =>  $users));
}

public function getdataAction(){
    return new ViewModel();
}

protected function getObjectManager()
{
    if (!$this->_objectManager) {
        $this->_objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
    }

    return $this->_objectManager;
}    
}

The directory structure is the next:

enter image description here


Solution

  •  'application' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/Application',
                    'defaults' => array(
                        __NAMESPACE__ => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
    

    This code is wrong you have to quote this NAMESPACE key

     'application' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/Application',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
    

    It will be fix this issue

    Index(resolves to invalid controller class or alias: Index)

    In the other hand you use this :

    'invokables' => array(
            'Application\Controller\Index' => Controller\IndexController::class
    

    You almost have this right, but you have to put the complete Full Qualified Class Name (FQCN)

    'invokables' => array(
            'Application\Controller\Index' => Application\Controller\IndexController::class
    

    EDIT :

    You define your main route as /Application and

    'defaults' => array(
                        __NAMESPACE__ => 'Application\Controller',
                        'controller'    => 'Index',
    

    ucfirst matters here try it.

    http://localhost/Application/Index/getdata
    

    and

    http://localhost/application/index/getdata