在自定义路由器PHP中匹配URL上的Param

问题描述 投票:1回答:1

我正在为此自定义路由器和自定义请求类添加功能,以便能够提供页面和json响应。

我被困在路由器部分,其中路由在URL中有一个参数,如:

example.com/apply/{variable}

这些是班级:

路由器类:

<?php

class Router
{
    private $request;

    private $supportedHttpMethods = array("GET", "POST");

    function __construct(RequestInterface $request)
    {
        $this->request = $request;
    }

    function __call($name, $args)
    {
        list($route, $method) = $args;
        if (!in_array(strtoupper($name), $this->supportedHttpMethods)) {
            $this->invalidMethodHandler();
        }
        $this->{strtolower($name)}[$this->formatRoute($route)] = $method;
    }

    /**
     * Removes trailing forward slashes from the right of the route.
     *
     * @param route (string)
     */
    private function formatRoute($route)
    {
        $result = rtrim($route, '/');
        if ($result === '') {
            return '/';
        }

        return $result;
    }

    private function invalidMethodHandler()
    {
        header("{$this->request->serverProtocol} 405 Method Not Allowed");
    }

    private function defaultRequestHandler()
    {
        header("{$this->request->serverProtocol} 404 Not Found");
    }

    /**
     * Resolves a route
     */
    function resolve()
    {
        $methodDictionary = $this->{strtolower($this->request->requestMethod)};
        $formatedRoute = $this->formatRoute($this->request->requestUri);
        $method = $methodDictionary[$formatedRoute];
        if (is_null($method)) {
            $this->defaultRequestHandler();

            return;
        }
        echo call_user_func_array($method, array(
            $this->request
        ));
    }

    function __destruct()
    {
        $this->resolve();
    }
} 

请求类:

<?php

include_once 'RequestInterface.php';

class Request implements RequestInterface
{
    private $params = [];

    public function __construct()
    {
        $this->bootstrapSelf();
    }

    private function bootstrapSelf()
    {
        foreach ($_SERVER as $key => $value) {
            $this->{$this->toCamelCase($key)} = $value;
        }
    }

    private function toCamelCase($string)
    {
        $result = strtolower($string);

        preg_match_all('/_[a-z]/', $result, $matches);
        foreach ($matches[0] as $match) {
            $c = str_replace('_', '', strtoupper($match));
            $result = str_replace($match, $c, $result);
        }

        return $result;
    }

    public function isPost()
    {
        return $this->requestMethod === "POST";
    }

    /**
     * Implemented method
     */
    public function getParams()
    {
        if ($this->requestMethod === "GET") {
            $params = [];
            foreach ($_GET as $key => $value) {
                $params[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
            }
            $this->params = array_merge($this->params, $params);
        }
        if ($this->requestMethod == "POST") {
            $params = [];
            foreach ($_POST as $key => $value) {
                $params[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
            }
            $this->params = array_merge($this->params, $params);
        }

        return $this->params;
    }
}

这就是我打电话给路由器的方式:

$router->get('/apply/{code}', function($request) use($myClass) {});

哪种方法会更好?我不知道如何解决这个问题。

php routing zend-expressive psr-7
1个回答
2
投票

我强烈建议在从头开始重新发明轮子之前先了解现有的http factory implementations。即使是自定义实现,它们看起来似乎也能在短期内提供一些灵活性和优势,您可以通过构建基于此类方法的应用程序轻松地在中/长期内开展自己的工作。

语言本身和PHP生态系统都进化了很多,我们是在2019年,我们有几十个编写良好,可重用的库。只需挑选你的武器,专注于你的真正目标。没有测试的任何代码,包括魔法,缺乏作曲家,正确的自动加载机制,编写良好的路由器或快速模板引擎;大多数时候会比它提供的价值更痛苦。我们应该停止重复自己。

据我所知,您的目标是在特定的URI路径上提供JSON内容,但您正在尝试发明一个路由器。如果您的目标是编写正确的路由器,则与提及的请求/响应接口无关。我建议看看一些可重用的,与框架无关的路由器的实现,如FastRouteZend RouterAura Router等,首先要有一个想法。实现一个合适的路由器当然不是火箭科学,但它并不像你可能意识到的那样简单。尽管如此,尝试编写该组件也可以具有教育意义,如果您的目标是这样,那就去做吧。

这里有几个提示(以及需要考虑的新问题):

  • 有一个PSR-15 Request Handler标准。在名为requestHandler的私有方法中发送标头可能不是一个好主意。
  • 请求处理程序和路由器是不同的组件,需要不同的工作流程。您正在将它们混合在代码中,这是一个警告标志。
  • 任何涉及__magic的代码都会为自己和潜在的未来开发人员设置一个陷阱。
  • 我不确定include_once 'RequestInterface'线的结果,但我们有HTTP Message interfaces。在处理请求时,我会考虑在任何类型的自定义实现中导入use Psr\Http\Message\ServerRequestInterface
  • __destruct回应也很有趣。你需要的是一个发射器。几个例子:Http EmitterZend Http Runner
  • 以下是您实际问题的高级答案:您需要实现一种机制(可能使用正则表达式)来捕获URI部分中的模式,并在“路径”中解析和检测可选或必需的命名部分。

就个人而言,我建议你看看Zend Expressive。在编写轻量级,中间件驱动的应用程序时,它可以帮助开发人员。 Expressive的最佳功能是您可以根据自己的需要选择任何武器。它不是一个成熟的MVC框架,提供了一种编写Web应用程序的新方法,而且速度非常快。例如,您可以自由选择所需的任何组件;用于呈现需求的Twig,用于CLI的Symfony控制台,用于依赖注入容器的Zend Service Manager,用于路由的Aura路由器等。

你可以只使用几个命令试试它(假设你有globally installed composer):

composer create-project zendframework/zend-expressive-skeleton my-app
cd my-app
composer run --timeout=0 serve

并打开您的浏览器:http://localhost:8080

祝好运!

© www.soinside.com 2019 - 2024. All rights reserved.