2013-08-28 117 views
0

所以我有一个新的ZF2安装,一切正常,除非我创建一个新的控制器... FooController.php和我去应用程序/富我得到一个404我不'弄不明白为什么,我必须设置的路线,在ZF1制定出箱Zend Framework 2路由不工作

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

回答

1

的是你需要设置在leaast一个途径。你可以建立一个通用的途径来处理控制器/动作类型的路由:

/** 
* 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', 
     ), 
    ), 
), 
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的方式,你可以自动完成,你必须为每个新控制器创建一个路由

+0

你不需要每个控制器的路由,你可以像上面一样捕获所有的控制器/操作 – Andrew