构建一个 PHP 路由器 [英] building a PHP router
问题描述
可能的重复:
将 URL 路由转换为函数参数 php mvc
MVC 中的 CMS 路由
我目前正在尝试重写一个 PHP 路由器.
I'm currently trying to rewrite a PHP router.
新的 htaccess 重写如下.
The new htaccess rewrite has the follows.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/index.php?url=$1 [L]
</IfModule>
在 public
的 index.php 中,我使用 $url = $_GET['url'];
Whilst in index.php in public
, I am getting the URL using the $url = $_GET['url'];
我需要做的是将 $url
传递给 Router 函数:: route($url)
What I need to do is to pass $url
to the Router function:: route($url)
如果一个 URL 被传递为:/page/function/$params 那么它会被转换为:index.php?url=page/xapp/function
,我需要映射和路由到控制器 xapp
并调用 function($params)
.
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 xapp
and call function($params)
.
此时,自动加载器已经被调用.如果仅调用/page/,我还需要设置要调用的默认函数.
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.
我如何在路由器中实现这一点?
How would I achieve this in a router?
推荐答案
你应该查看 klein 的代码.php,一个小型的 php 路由器.我认为你应该从那个解决方案中弄清楚.
You should check out the code of klein.php, a small php router. I think you should figure it from that solution.
如果没有,也请查看slim这里
这篇关于构建一个 PHP 路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!