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

查看:63
本文介绍了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.

现在,我想创建一个URL路由类,该类可以由APP编写器(可能还有CMS App用户)扩展,但是我正在尝试找到最佳的实现方法通过查看其他应用程序是如何做到的.

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来制作自己的格式,因为这是众所周知的.我写了一个小类,使我可以嵌套这些正则表达式路由表.我曾经使用过通过继承实现的类似方法,但是它不需要继承,所以我重写了它.

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",
    ));
}

此外,您还可以在右侧定义所需的路由字符串默认值.只是不要忘记将它们记录下来,否则您将使人们感到困惑.如果您在右侧未包含函数名称,则当前正在调用索引. 此处是我当前的代码.您可能需要更改它以处理您喜欢的错误和/或默认操作.

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天全站免登陆