0

所以我有一个全新的 ZF2 安装,一切正常,除非我创建一个新的控制器...... FooController.php 我去 application/foo 我得到一个 404 我不明白为什么,我必须在开箱即用的 ZF1 中设置路线

<?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;
    }
}
4

2 回答 2

1

是的,您至少需要设置一条路线。您可以设置通用路由来处理控制器/动作类型路由:

/**
 * 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',
        ),
    ),
),
于 2013-08-28T13:06:58.230 回答
0

解决方案是创建一条这样的新路线:

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

我认为这就是它在 ZF2 中的工作方式,你可以自动完成,你必须为每个新控制器创建一个路由

于 2013-08-28T13:12:47.383 回答