Zend Framework 2中的模块内基于约定的自动路由可能吗? [英] Automatic convention-based routing within a module in Zend Framework 2--possible?

本文介绍了Zend Framework 2中的模块内基于约定的自动路由可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解使路由在Zend Framework 2中正常工作所需的所有配置,我不禁想知道是否使此操作变得不必要的复杂.

I'm trying to understand all the configuration necessary to get my routing working in Zend Framework 2, and I can't help but wonder if I am making this more complicated than necessary.

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

I am working on a simple app that will follow a very simple convention:

/:module/:controller/:action

/:module/:controller/:action

我已经创建并连接了模块"svc"(服务"的缩写).然后,我创建了第二个控制器"ClientsController",我无法获得路由以将请求传递给例如/svc/clients/list到ClientsController :: listAction().

I've already created and wired up my module, "svc" (short for "service)". I then created a second controller, the "ClientsController", and I can't get the routing to pass through my requests to, e.g., /svc/clients/list to ClientsController::listAction().

当我在深度嵌套数组中遍历数百行配置时,我在想-没有某种方法可以将URL默认映射到/:module/:controller/:行动吗?

As I'm wading through hundreds of lines of configuration, in deeply nested arrays, I'm thinking--isn't there some way to just have a default mapping of my URLs to /:module/:controller/:action ?

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

Thanks for any assistance. I'm going off of the Zend Framework 2 Quick Start, which walked me through creating a new module and then adding a controller to that module. But when I tried to add second controller to that module, I am tripping over the routing.

更新:第一次我没有注意到这一点,但是显然这应该是Zend Framework Skeleton应用程序的功能.从快速入门指南中:

Update: I didn't catch this the first time through, but apparently this is supposed to be a feature of the Zend Framework Skeleton app. From the quick start guide:

ZendSkeletonApplication附带了一个默认路线",该路线可能会 让您执行此操作.那条路线基本上期望 "/{module}/{controller}/{action}",可让您指定以下内容: "/zend-user/hello/world"

ZendSkeletonApplication ships with a "default route" that will likely get you to this action. That route basically expects "/{module}/{controller}/{action}", which allows you to specify this: "/zend-user/hello/world"

这正是我想要的!但是我无法正常工作.

That's exactly what I want! But I can't get it to work.

它列出了一个不完整的module.config.php,并在底部列出了有关在此处放置其他配置"的注释.我试图弄清楚其他配置"是什么,然后总结一下:

It lists an incomplete module.config.php, with a comment at the bottom about putting "other configuration" here. I tried to figure out what that "other configuration" is, and wound up with this:

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,这就是我的控制器的样子.

JFYI, here is what my controller looks like.

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();
    }
}

我的路线不起作用.当我尝试拉出任何一条路线时,我得到找不到路线".

My routes are not working. I get "route not found" when I try to pull up any of my routes.

推荐答案

它列出了一个不完整的module.config.php,并在底部列出了有关在此处放置其他配置"的注释.我试图弄清楚其他配置"是什么,然后总结一下:

It lists an incomplete module.config.php, with a comment at the bottom about putting "other configuration" here. I tried to figure out what that "other configuration" is, and wound up with this:

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

If your module.config.php really looks like that then it won't work, routes is an array of routes defined in the router key, your config contains no such spec, try replacing it with this

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

这篇关于Zend Framework 2中的模块内基于约定的自动路由可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆