URL Router和Dispatcher有什么区别? [英] What is the difference between URL Router and Dispatcher?

查看:90
本文介绍了URL Router和Dispatcher有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道URL Router和Dispatcher之间的区别,因为在Internet上进行搜索有一些有趣的事情,只是不知道这是因为它们相似还是因为它们的功能相反.谁能告诉我这是什么,每个是什么,还有一个例子?

I want to know the difference between the URL Router and Dispatcher, because searching on the internet has some interesting things, just do not know if it's because they are similar, or because they reverse the function of each. Can anyone tell me what it is and what each one, and an example?

我不知道从URL路由器到Dispatcher的区别,也不知道它们在Internet上存在的内容问题,有时似乎Dispatcher是Router,而Router似乎是Dispatcher,最终不知道每个路由器的权利是什么,每一个是什么,以及如何实现每个.

I do not know the differentiate from URL Router to Dispatcher, the question of content they have on the Internet, sometimes it seems the Dispatcher is the Router, and the Router seems Dispatcher, and end up not knowing what the right of each, what is each one, and how implement each one.

谢谢.

推荐答案

框架和库如何解释Router和Dispatcher的职责将有所不同.我在下面详细介绍的是如何解释这两项服务的职责.并不是说这是解释它的唯一方法,或者其他解释是错误的.

How frameworks and libraries interpret the responsibilities of the Router and Dispatcher are going to be different. What I detail below is how I interpret the responsibilities of these two services. It is not to say that this is the only way to interpret it or that other interpretations are wrong.

这有点像在加油站或便利店问路.您正在穿过城镇,您需要弄清楚如何前往最近的酒店.您进去问服务员,他们会指出您正确的方向,或者至少您希望方向正确.路由就是这样.请求资源时,路由器会提供请求以达到正确资源所需的指示.

This is kinda like asking for directions at a gas station or convenience store. You're going through town and you need to figure out how to get to the nearest hotel. You go in and ask the attendant and they point you off in the correct direction, or at least you hope the directions are correct. Routing is exactly this. A request comes in for a resource, the Router provides the directions necessary for the request to reach the correct resource.

在大多数主要框架中,您都会将特定的请求URL路由到将被调用以完成请求的对象和方法.通常,您会看到路由器也从URL解析出动态参数.例如,如果您通过/users/1234访问用户,其中1234是用户ID,则路由器将解析出ID部分,并将其作为指向资源的一部分提供.

In most major frameworks you're going to be routing a specific request URL to an object and method that will be invoked to complete the request. Often times you'll see the Router also parsing out dynamic arguments from the URL. For example, if you accessed users via /users/1234 where 1234 is the user ID the Router would parse out the ID portion and provide this as part of the directions to the resource.

调度使用路由"步骤中的信息来实际生成资源.如果路由"步骤要求提供指导,则分派"是遵循这些指导的实际过程.调度只知道从路由器获得指示后,才能确切知道要创建什么以及生成资源所需的步骤.

Dispatching uses the information from the Routing step to actually generate the resource. If the Routing step is asking for directions then Dispatching is the actual process of following those directions. Dispatching knows exactly what to create and the steps needed to generate the resource, but only after getting the directions from the Router.

这些示例实现故意非常简单且幼稚.如果不进行重大改进,您就不想在任何生产环境中使用它.

These example implementations are intentionally very simple and naive. You would not want to use this in any kind of production environment without drastic improvements.

在此示例中,我们将路由到可调用函数,而不是路由到对象和方法.这也说明您不需要 路由到对象.只要调度程序可以正确获取正确的资源,您就可以将其路由到所需的任何数据.

In this example instead of routing to an object and method we're just gonna route to a callable function. This also demonstrates that you don't need to route to an object; as long as the dispatcher can properly get the correct resource you can route to whatever data you want.

首先,我们需要采取一些应对措施.让我们创建一个可以匹配的简单Request对象.

First we need something to route against. Let's create a simple Request object that we can match against.

<?php

class Request {

    private $method;
    private $path;

    function __construct($method, $path) {
        $this->method = $method;
        $this->path = $path;
    }

    function getMethod() {
        return $this->method;
    }

    function getPath() {
        return $this->path;
    }

}

现在我们可以匹配某些东西了,让我们来看一个简单的Router实现.

Now that we can match against something let's take a look at a simple Router implementation.

<?php

class Router {

    private $routes = [
        'get' => [],
        'post' => []
    ];

    function get($pattern, callable $handler) {
        $this->routes['get'][$pattern] = $handler;
        return $this;
    }

    function post($pattern, callable $handler) {
        $this->routes['post'][$pattern] = $handler;
        return $this;
    }

    function match(Request $request) {
        $method = strtolower($request->getMethod());
        if (!isset($this->routes[$method])) {
            return false;
        }

        $path = $request->getPath();
        foreach ($this->routes[$method] as $pattern => $handler) {
            if ($pattern === $path) {
                return $handler;
            }
        }

        return false;
    }

}

现在我们需要某种方法来为给定请求调用已配置的$handler.

And now we need some way to invoke a configured $handler for a given Request.

<?php

class Dispatcher {

    private $router;

    function __construct(Router $router) {
        $this->router = $router;
    }

    function handle(Request $request) {
        $handler = $this->router->match($request);
        if (!$handler) {
            echo "Could not find your resource!\n";
            return;
        }

        $handler();
    }

}

现在,让我们将它们放在一起,并展示如何使用这些简单的实现.

Now, let's bring it all together and show how to use these simple implementations.

<?php

$router = new Router();
$router->get('foo', function() { echo "GET foo\n"; });
$router->post('bar', function() { echo "POST bar\n"; });

$dispatcher = new Dispatcher($router);

$dispatcher->handle(new Request('GET', 'foo'));
$dispatcher->handle(new Request('POST', 'bar'));
$dispatcher->handle(new Request('GET', 'qux'));

您可以通过查看 http://3v4l.org/gbsoJ 来查看此实施中的示例.

You can see an example of this implementation in action by checking out http://3v4l.org/gbsoJ.

该示例实现应传达路由和调度的概念.实际上,执行这些动作比我的示例要多得多.路由器通常会使用正则表达式来与请求进行匹配,并且在匹配时可能会查看其他请求属性.此外,您还将看到一些库利用与路由器交互的解析器,因此您不仅可以传递可调用的函数,还可以传递更多信息.基本上,解析程序将确保与$handler相匹配的$handler可以转换为可调用的函数.

This example implementation is supposed to communicate the concept of routing and dispatching. Really there's a lot more to performing these actions than my example. Often the Router will use regex to match against a Request and may look at other Request attributes when matching. Additionally you'll see some libraries utilizing a resolver that interacts with the Router so that you can pass more than just callable functions. Basically, the Resolver would ensure that the $handler matched against can be turned into an invokable function.

此外,您应该考虑使用许多示例和实现来代替.对于我的个人项目,我喜欢 FastRoute ,因为它易于使用且性能出色.但是,几乎所有主要框架都有自己的实现.您也应该检查一下.

Also, there's plenty of examples and implementations for this that you should look at using instead. For my personal projects I like FastRoute for its ease of use and performance. But, nearly all major frameworks have their own implementations. You should check those out too.

这篇关于URL Router和Dispatcher有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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