PHP路由系统

发布于 2024-11-17 07:24:25 字数 1223 浏览 2 评论 0原文

我正在尝试创建一个基于注释的路由系统(类似于 Recess Framework< /a>)。

<?php

class MyController extends ActionController {

    /** !Route GET /hello/$firstname/$lastname **/
    public function helloAction($firstname, $lastname) {
        echo('Hello '.$firstname.' '.$lastname);
    }
}

?>

如果我去 http://domain.com/hello/James/Bond 我得到

Hello James Bond

所以我有两个问题:

1)这是一个好主意吗?与集中式路由系统(如 Zend Framework)的优缺点。也许我没有看到这种路由技术后来出现的问题。

2)如果路由中有正则表达式,如何检查重复路由

<?php

class MyController extends ActionController {

    /** 
     *!Route GET /test/$id = {
     *    id: [a-z0-9]
     *}
     **/
    public function testAction($id) {
        echo($id);
    }

    /** 
     *!Route GET /test/$id = {
     *    id: [0-9a-z]
     *}
     **/
    public function otherTestAction($id) {
        echo($id);
    }
}

?>

我得到两条路由: /test/[a-z0-9]//test/[0-9a-z ]/ 如果我去 http://domain.com/test/a12/ 两条路线都是有效的。

谢谢 :)

I'm trying to create a routing system based on annotations (something like on Recess Framework).

<?php

class MyController extends ActionController {

    /** !Route GET /hello/$firstname/$lastname **/
    public function helloAction($firstname, $lastname) {
        echo('Hello '.$firstname.' '.$lastname);
    }
}

?>

If I go to http://domain.com/hello/James/Bond I get

Hello James Bond

So I have two questions:

1) Is it a good idea? Pros and cons vs centralized routing system (like Zend Framework). Maybe I don't see problems that my arise later with this routing technique.

2) How to check for duplicate routes if there is regexp in routes

<?php

class MyController extends ActionController {

    /** 
     *!Route GET /test/$id = {
     *    id: [a-z0-9]
     *}
     **/
    public function testAction($id) {
        echo($id);
    }

    /** 
     *!Route GET /test/$id = {
     *    id: [0-9a-z]
     *}
     **/
    public function otherTestAction($id) {
        echo($id);
    }
}

?>

I get two routes: /test/[a-z0-9]/ and /test/[0-9a-z]/ and if i go to http://domain.com/test/a12/ both routes are valid.

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

庆幸我还是我 2024-11-24 07:24:25

尝试使用 Java 注释格式,它应该是更容易统一解析。

它看起来像这样:

<?php   
class MyController extends ActionController {

    /**
       @Route(get=/hello/$firstname/$lastname)
       @OtherVal(var1=2,var2=foo)
       @OtherVal2
    **/
    public function helloAction($firstname, $lastname) {
        echo('Hello '.$firstname.' '.$lastname);
    }
}
?>

并使用以下正则表达式解析您的注释:

@(?P<annotation>[A-Za-z0-9_]*)(\((?P<params>[^\)]*))?

当然,尽可能缓存这些以避免重复解析。

Try using the Java annotation format which should be much easier to parse uniformly.

It looks something like this:

<?php   
class MyController extends ActionController {

    /**
       @Route(get=/hello/$firstname/$lastname)
       @OtherVal(var1=2,var2=foo)
       @OtherVal2
    **/
    public function helloAction($firstname, $lastname) {
        echo('Hello '.$firstname.' '.$lastname);
    }
}
?>

And parse your annotation out with the following regex:

@(?P<annotation>[A-Za-z0-9_]*)(\((?P<params>[^\)]*))?

And of course cache these where possible to avoid repeated parsing.

折戟 2024-11-24 07:24:25

缺点:

  • 可能很难全面了解服务器中所有方法的 URL 映射。
  • 要更改 URL,您必须更改源代码,映射并不与应用程序分离。

如果方法签名和映射始终与示例一样相关,您可以使用反射来提取映射,其中 helloAction 被选取为 /hello 并且每个方法参数都是一个子目录按照定义的顺序排列。

那么注释就不需要复制 URL,只需要复制该方法是一个端点这一事实,如下所示:

<?php
    class MyController extends ActionController {

        /** !endpoint(method=GET) **/
        public function helloAction($firstname, $lastname) {
            echo('Hello '.$firstname.' '.$lastname);
        }
}

Cons:

  • It may be difficult to keep an overview of URL mappings of all methods in your server.
  • To change a URL you have to change the source code, mapping is not separated from the application.

If the method signature and mapping are always as related as the example you might use reflection to extract the mapping where helloAction is picked up as /hello and each method argument is a subdirectory of this in the order as they're defined.

Then the annotation wouldn't need to duplicate the URL, only the fact that the method is an endpoint, something like this:

<?php
    class MyController extends ActionController {

        /** !endpoint(method=GET) **/
        public function helloAction($firstname, $lastname) {
            echo('Hello '.$firstname.' '.$lastname);
        }
}
冷月断魂刀 2024-11-24 07:24:25
  1. 我认为这是一个好主意/解耦代码和入口点似乎到处都使用

  2. 通​​常你不检查对于它:第一个匹配的获胜。

  1. I think it's a good idea / Decoupling code and entry point seems pretty much used everywhere

  2. Usually you don't check for it: the first one that matches wins.

欢你一世 2024-11-24 07:24:25

这样做是一个好主意,只要您在生产中缓存已编译的路由。路由时解析文件会产生相关成本,因此您希望在不开发时避免这种情况。

至于检查重复项,不要通过比较声明来检查。只需在路由时检查即可。如果两个规则匹配,则抛出DuplicateRouteException。因此,在路由 http://domain.com/test/a12/ 时,您会看到两条路由均有效并抛出异常。

Doing so is a great idea, as long as you cache the compiled routes in production. There is a cost associated to parsing your files when routing so you want to avoid that when not developing.

As for checking for duplicates, don't check by comparing the declaration. Simply check when routing. If two rules matches, throw a DuplicateRouteException. So, when routing http://domain.com/test/a12/, you'll see that both routes are valid and throw the exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文