构建一个 PHP 路由器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13432016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:24:21  来源:igfitidea点击:

building a PHP router

phpurl-routing

提问by bear

Possible Duplicate:
turn URL route into funciton arguments php mvc
CMS Routing in MVC

可能的重复:
将 URL 路由转换为函数参数 php mvc
CMS Routing in MVC

I'm currently trying to rewrite a PHP router.

我目前正在尝试重写一个 PHP 路由器。

The new htaccess rewrite has the follows.

新的 htaccess 重写如下。

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^$    public/    [L]
    RewriteRule    (.*) public/index.php?url=    [L]
</IfModule>

Whilst in index.php in public, I am getting the URL using the $url = $_GET['url'];

在 index.php 中public,我使用$url = $_GET['url'];

What I need to do is to pass $urlto the Router function:: route($url)

我需要做的是传递$url给路由器功能::route($url)

If a URL is passed as : /page/function/$params which would then translate as : index.php?url=page/xapp/function, I'd need to map and route to Controller xappand call function($params).

如果 URL 作为 : /page/function/$params 传递,然后将转换为 : index.php?url=page/xapp/function,我需要映射并路由到 Controllerxapp并调用function($params).

By this time, the autoloader has already been called. I'd also need to set a default function to be called if only /page/ is called.

此时,自动加载器已经被调用。如果仅调用 /page/ ,我还需要设置要调用的默认函数。

How would I achieve this in a router?

我将如何在路由器中实现这一点?

回答by tonino.j

You should check out the code of klein.php, a small php router. I think you should figure it from that solution.

您应该查看klein.php的代码,是一个小型 php 路由器。我认为你应该从那个解决方案中弄清楚。

If not, check out also slimhere

如果没有,请查看这里也很