0

我试图了解让我的路由在 Zend Framework 2 中工作所需的所有配置,我不禁想知道我是否让这变得比必要的更复杂。

我正在开发一个简单的应用程序,它将遵循一个非常简单的约定:

/:模块/:控制器/:动作

我已经创建并连接了我的模块“svc”(“服务”的缩写)。然后我创建了第二个控制器,即“ClientsController”,但我无法通过路由将我的请求传递到例如 /svc/clients/list 到 ClientsController::listAction()。

当我涉足数百行配置时,在深度嵌套的数组中,我在想——难道没有某种方法可以将我的 URL 默认映射到 /:module/:controller/:action 吗?

感谢您的任何帮助。我将离开Zend Framework 2 Quick Start,它引导我创建一个新模块,然后向该模块添加一个控制器。但是当我尝试向该模块添加第二个控制器时,我在路由上绊倒了。

更新:我第一次没有发现这个,但显然这应该是 Zend Framework Skeleton 应用程序的一个特性。从快速入门指南:

ZendSkeletonApplication 附带了一个“默认路由”,可能会让您执行此操作。该路由基本上需要“/{module}/{controller}/{action}”,它允许您指定:“/zend-user/hello/world”</p>

这正是我想要的!但我无法让它工作。

它列出了一个不完整的 module.config.php,底部有一条关于将“其他配置”放在这里的注释。我试图弄清楚“其他配置”是什么,并以这个结束:

return array(
    'svc' => array(
        'type'    => 'Literal',
        'options' => array(
            'route'    => '/svc',
            'defaults' => array(
                'controller'    => 'svc\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(
                    ),
                ),
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'svc\Controller\Clients' => 'svc\Controller\ClientsController',
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'album' => __DIR__ . '/../view',
        ),
    ),
);

JFYI,这是我的控制器的样子。

namespace svc\Controller;

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

class ClientsController extends AbstractActionController {

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

    public function anotherAction(){

        return new ViewModel();
    }
}

我的路线不起作用。当我尝试拉起我的任何路线时,我得到“找不到路线”。

4

1 回答 1

0

它列出了一个不完整的 module.config.php,底部有一条关于将“其他配置”放在这里的注释。我试图弄清楚“其他配置”是什么,并以这个结束:

如果您的 module.config.php 真的看起来像那样,那么它将不起作用,routes是键中定义的路由数组router,您的配置不包含此类规范,请尝试将其替换为

return array(
    // routes
    'router' => array(
        'routes' => array(
            'svc' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/svc',
                    'defaults' => array(
                        'controller'    => 'svc\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(
                                 // add the default namespace for :controllers in this route
                                 '__NAMESPACE__' => 'svc\Controller',
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),    
    'controllers' => array(
        'invokables' => array(
            'svc\Controller\Clients' => 'svc\Controller\ClientsController',
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'album' => __DIR__ . '/../view',
        ),
    ),
);
于 2013-05-22T12:22:44.980 回答