简体   繁体   English

ZF2自定义路由

[英]ZF2 custom routing

I am trying to setup routing for our ZF2 application. 我正在尝试为我们的ZF2应用程序设置路由。

We have URLS like this: 我们有这样的URL:

/foo/bar.json            >>> should call /Controller/Foo/BarController.php
/foo/bar/123.json        >>> should call /Controller/Foo/BarController.php
/foo/baz/bar.json        >>> should call /Controller/FooController.php

and the underlaying controller structure 和底层控制器结构

/Controller/Foo/BarController.php
/Controller/FooController.php

It should route by looking up in the directory structure. 它应该通过在目录结构中查找来进行路由。 So /foo/baz/bar.json should look if /Controller/Foo/Baz/BarController.php exists. 因此/foo/baz/bar.json应该查看/Controller/Foo/Baz/BarController.php存在。 If not, look if /Controller/Foo/BazController.php exists, else look for /Controller/FooController.php and else give a 404 如果不存在,请查看是否存在/Controller/Foo/BazController.php ,否则请查找/Controller/FooController.php并给出404

However i do not seem to get mutch useful information from the zend documentation. 但是我似乎没有从zend文档中获得有用的信息。 I looked at the existing routers but it does not seem to me that any of these match the required functionality. 我看了看现有的路由器,但在我看来,这些路由器都不符合所需的功能。

Also there does not seem to be any information in the zend docs about creating a custom router. 在zend文档中似乎也没有关于创建自定义路由器的任何信息。

I tried to extend the Literal router and create one of my own by overriding the match() function and returning the RouteMatch object if a match was found, but it still gives me a 404 with a notice that thee was no matched route. 我试图通过覆盖match()函数并返回RouteMatch对象(如果找到match()来扩展Literal路由器并创建我自己的路由器,但是它仍然给我一个404,提示您没有匹配的路由。

The module.config file was ofcourse also edited to add the new router. 当然也对module.config文件进行了编辑以添加新的路由器。 and added it to the invokables 并将其添加到发票中

Can anyone tell me some starters about how to write a custom router or does anyone know any good information about doing this? 谁能告诉我一些有关如何编写自定义路由器的入门资料,或者有人知道这样做的任何好信息吗?

Not sure if it is the best way. 不知道这是否是最好的方法。

You can point all your URLS to one controller eg /Controller/RouteController.php. 您可以将所有URL指向一个控制器,例如/Controller/RouteController.php。 And there you can traverse the url and dispath the appropriate controller: 在这里您可以遍历url并使适当的控制器失去路径:

public function indexAction()
{
    $event = $this->getEvent();
    $routeMatch = $event->getRouteMatch();

    $locator = $this->getServiceLocator();
    if ($locator->has('ControllerLoader')) {
        $locator = $locator->get('ControllerLoader');
    }

    $url = parse_url($this->getRequest()->getUri());
    $path = str_replace('.json', '', $url['path']);
    $path = explode('/', trim($path, '/'));

    while (sizeof($path)){
        $controllerName = 'Controller\\' .implode('\\', $path);
        if ($locator->has($controllerName)) {
            return $this->forward()->dispatch($controllerName, $routeMatch->getParams());
        }

        array_pop($path);
    }

    //not found
}

Be sure to define your controllers in module config: 确保在模块配置中定义控制器:

array(
  'controllers' => array(
    'invokables' => array(
        'Controller\Foo\Baz' => 'Controller\Foo\BazController',
        ...
    )
  ),
);

First, I don't think you mean directly looking up the directory structure, are you? 首先,我认为您的意思不是直接查找目录结构,是吗? You should never be doing that. 您绝对不应该那样做。 If you've configured your application properly, you won't need it since zf2 handles the autoloading for you. 如果您已经正确配置了应用程序,则不需要它,因为zf2为您处理了自动加载。

Anyway, there are a few ways to achieve this. 无论如何,有几种方法可以实现这一目标。

  • Indeed create your customized Route class 确实创建了定制的Route类
  • Match anything that start with /foo and route it to fe FooDispatchController in which you further analyze the RouteMatch object and forward() to the proper controller from there. 匹配以/ foo开头的任何内容,并将其路由到fe FooDispatchController,在其中您进一步分析RouteMatch对象,然后从那里forward()到适当的控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM