Uffo
Uffo

Reputation: 10046

Zend Framework 2 Routes not working

So I have a new and fresh installation of ZF2, everything works, except if I create a new controller... FooController.php and I go to application/foo I get a 404 I don't get it why, do I have to setup routes, in ZF1 that worked out of the box

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

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

class FooController extends AbstractActionController
{
    public function indexAction()
    {
        $view = new ViewModel(array(
            'foo' => 'The Foo Controller'
        ));
        return $view;
    }
}

Upvotes: 0

Views: 1208

Answers (2)

Uffo
Uffo

Reputation: 10046

The solution was to create a new route like this:

    'foo' => array(
        'type' => 'Zend\Mvc\Router\Http\Literal',
        'options' => array(
            'route'    => '/foo',
            'defaults' => array(
                '__NAMESPACE__' => 'Application\Controller',
                'controller' => 'Application\Controller\Foo',
                'action'     => 'index',
            ),
        ),
    ),

'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController',
        'Application\Controller\Foo' => 'Application\Controller\FooController',
    ),

I think this is the way it works in ZF2, you can do it automatically, you have to create a route for each new controller

Upvotes: 0

Andrew
Andrew

Reputation: 12809

Yes you need to setup at leaast one route. You can setup a generic route to handle controller/action type routing:

/**
 * Generic Route
 */
'generic_route' => array(
    'type'    => 'segment',
    'options' => array(
        'route'    => '[/:controller[/:action[/:id[/:extra]]]][/]',
        'constraints' => array(
            '__NAMESPACE__' => 'Application\Controller',
            'action'        => '[a-zA-Z][a-zA-Z0-9_-]*',
            'controller'    => '[a-zA-Z][a-zA-Z0-9_-]*',
            'id'            => '[0-9]+',
            'extra'         => '[a-zA-Z0-9_-]+',
        ),
        'defaults' => array(
            'controller' => 'Index',
            'action'     => 'index',
        ),
    ),
),

Upvotes: 1

Related Questions