PHP 应用程序 URL 路由 [英] PHP Application URL Routing

查看:27
本文介绍了PHP 应用程序 URL 路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在编写一个框架,我想在该框架上建立一些我正在开发的应用程序(该框架在那里,所以我有一个可以使用的环境,一个系统可以让我,例如,使用单点登录)

So I'm writing a framework on which I want to base a few apps that I'm working on (the framework is there so I have an environment to work with, and a system that will let me, for example, use a single sign-on)

我想制作这个框架,它的应用程序使用面向资源的架构.

I want to make this framework, and the apps it has use a Resource Oriented Architecture.

现在,我想创建一个可由 APP 编写者扩展的 URL 路由类(也可能由 CMS 应用程序用户扩展,但这是未来的 WAYYYY),我正在尝试找出最好的方法来做到这一点看看其他应用是如何做到的.

Now, I want to create a URL routing class that is expandable by APP writers (and possibly also by CMS App users, but that's WAYYYY ahead in the future) and I'm trying to figure out the best way to do it by looking at how other apps do it.

推荐答案

我更喜欢使用 reg ex 而不是制作我自己的格式,因为它是常识.我写了一个我使用的小类,它允许我嵌套这些 reg ex 路由表.我以前用过类似的东西,是通过继承实现的,但是不需要继承,所以重写了.

I prefer to use reg ex over making my own format since it is common knowledge. I wrote a small class that I use which allows me to nest these reg ex routing tables. I use to use something similar that was implemented by inheritance but it didn't need inheritance so I rewrote it.

我对一个键执行正则表达式并映射到我自己的控制字符串.以下面的例子为例.我访问 /api/related/joe 并且我的路由器类创建了一个新对象 ApiController 并调用它的方法 relatedDocuments(array('tags' => 'joe'));

I do a reg ex on a key and map to my own control string. Take the below example. I visit /api/related/joe and my router class creates a new object ApiController and calls it's method relatedDocuments(array('tags' => 'joe'));

// the 12 strips the subdirectory my app is running in
$index = urldecode(substr($_SERVER["REQUEST_URI"], 12)); 

Route::process($index, array(
    "#^api/related/(.*)$#Di"    => "ApiController/relatedDocuments/tags",

    "#^thread/(.*)/post$#Di"    => "ThreadController/post/title",
    "#^thread/(.*)/reply$#Di"   => "ThreadController/reply/title",
    "#^thread/(.*)$#Di"         => "ThreadController/thread/title",

    "#^ajax/tag/(.*)/(.*)$#Di"  => "TagController/add/id/tags",
    "#^ajax/reply/(.*)/post$#Di"=> "ThreadController/ajaxPost/id",
    "#^ajax/reply/(.*)$#Di"     => "ArticleController/newReply/id",
    "#^ajax/toggle/(.*)$#Di"    => "ApiController/toggle/toggle",

    "#^$#Di"                    => "HomeController",
));

为了减少错误和简化,您可以细分表格.这样你就可以把路由表放到它控制的类中.以上面的示例为例,您可以将三个线程调用合并为一个.

In order to keep errors down and simplicity up you can subdivide your table. This way you can put the routing table into the class that it controls. Taking the above example you can combine the three thread calls into a single one.

Route::process($index, array(
    "#^api/related/(.*)$#Di"    => "ApiController/relatedDocuments/tags",

    "#^thread/(.*)$#Di"         => "ThreadController/route/uri",

    "#^ajax/tag/(.*)/(.*)$#Di"  => "TagController/add/id/tags",
    "#^ajax/reply/(.*)/post$#Di"=> "ThreadController/ajaxPost/id",
    "#^ajax/reply/(.*)$#Di"     => "ArticleController/newReply/id",
    "#^ajax/toggle/(.*)$#Di"    => "ApiController/toggle/toggle",

    "#^$#Di"                    => "HomeController",
));

然后你定义 ThreadController::route 就像这样.

Then you define ThreadController::route to be like this.

function route($args) {
    Route::process($args['uri'], array(
        "#^(.*)/post$#Di"    => "ThreadController/post/title",
        "#^(.*)/reply$#Di"   => "ThreadController/reply/title",
        "#^(.*)$#Di"         => "ThreadController/thread/title",
    ));
}

您还可以为右侧的路由字符串定义任何您想要的默认值.只是不要忘记记录它们,否则你会混淆人们.如果您没有在右侧包含函数名称,我目前正在调用 index .这里是我当前的代码.您可能希望更改它以处理您喜欢的错误和/或默认操作.

Also you can define whatever defaults you want for your routing string on the right. Just don't forget to document them or you will confuse people. I'm currently calling index if you don't include a function name on the right. Here is my current code. You may want to change it to handle errors how you like and or default actions.

这篇关于PHP 应用程序 URL 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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