Heim  >  Artikel  >  Backend-Entwicklung  >  php路由怎么实现

php路由怎么实现

WBOY
WBOYOriginal
2016-06-06 20:46:241746Durchsuche

php如何实现下列url格式路由分发:

app目录
┡┈▓ App
┊ ┡┈▓ Config //配置文件目录
┊ ┡┈▓ Controller //控制器目录
┊ ┊ ┡┈▒ Index.php // 默认控制器
┊ ┟┈▓ Modle // 模型目录
┊ ┟┈▓ View // 视图目录
┊ ┟┈▓ Module //模块目录
┊ ┊ ┟┈▓ Admin
┊ ┊ ┊ ┡┈▓ Controller
┊ ┊ ┊ ┊ ┗┈▒ Index.php
┊ ┊ ┊ ┟┈▓ Modle
┊ ┊ ┊ ┗┈▓ View
┊ ┊ ┟┈▓ Member
┊ ┊ ┊ ┡┈▓ Controller
┊ ┊ ┊ ┊ ┗┈▒ Index.php
┊ ┊ ┊ ┟┈▓ Modle
┊ ┊ ┊ ┗┈▓ View

  1. http://www.xxx.com/category/list/order/hot/
    http://www.xxx.com/admin/category/edit/
    http://www.xxx.com/member/user/id/3/

  2. http://www.xxx.com/index.php?c=category&a=list&order=hot
    http://www.xxx.com/index.php?m=admin&c=category&a=edit
    http://www.xxx.com/index.php?m=member&c=user&id=3

第1个url为前端访问,category为控制器,list为category类下的方法
第2个后台,admin为模块名,category为控制器,edit为category类下的方法

现在路由分发要实现以上2种形式的url的,怎样做?

回复内容:

php如何实现下列url格式路由分发:

app目录
┡┈▓ App
┊ ┡┈▓ Config //配置文件目录
┊ ┡┈▓ Controller //控制器目录
┊ ┊ ┡┈▒ Index.php // 默认控制器
┊ ┟┈▓ Modle // 模型目录
┊ ┟┈▓ View // 视图目录
┊ ┟┈▓ Module //模块目录
┊ ┊ ┟┈▓ Admin
┊ ┊ ┊ ┡┈▓ Controller
┊ ┊ ┊ ┊ ┗┈▒ Index.php
┊ ┊ ┊ ┟┈▓ Modle
┊ ┊ ┊ ┗┈▓ View
┊ ┊ ┟┈▓ Member
┊ ┊ ┊ ┡┈▓ Controller
┊ ┊ ┊ ┊ ┗┈▒ Index.php
┊ ┊ ┊ ┟┈▓ Modle
┊ ┊ ┊ ┗┈▓ View

  1. http://www.xxx.com/category/list/order/hot/
    http://www.xxx.com/admin/category/edit/
    http://www.xxx.com/member/user/id/3/

  2. http://www.xxx.com/index.php?c=category&a=list&order=hot
    http://www.xxx.com/index.php?m=admin&c=category&a=edit
    http://www.xxx.com/index.php?m=member&c=user&id=3

第1个url为前端访问,category为控制器,list为category类下的方法
第2个后台,admin为模块名,category为控制器,edit为category类下的方法

现在路由分发要实现以上2种形式的url的,怎样做?

php的路由必须和服务器的伪静态规则配合才能生效。

php里的路由的意思是地址转发,那么可以在生成URL的函数里传入各个参数生成一个新的的地址(而这个地址也就是伪静态地址)

我自己写的路由,希望对你有帮助。http://mengkang.net/17.html

如果你懒得看,说下大体思路:

第一、服务器上apache或者nginx把伪静态rewrite到实际地址
第二、在PHP生成URL地址的时候做url的规则(而这个规则就路由文件)替换
第三、保证用户访问的一定是伪静态地址,当用户访问非伪静态地址的时候PHP端根据$_SERVER['REQUEST_URI']判断其是否非伪静态地址,如果不是就跳转到路由表规定的伪静态地址

希望对楼主有帮助

单一路由规则,将请求url全部转发给index.php处理.
这种是单一入口的,也是最简单。

但一入口 比较通用的路由规则

<code># GreenCMS Rewrite规则
<ifmodule mod_rewrite.c>
   RewriteEngine on
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</ifmodule>
</code>

推荐一个精简的Router库做路由控制器 https://github.com/lloydzhou/router,可以根据映射的handler自动从request获取变量,支持自定义error handler和hook。可以通过hook方便的定制参数过滤、登录检查等。

<code>(new Router())
->error(405, function($message){
    header('Location: /hello/world', true, 302);
})
->get('/hello/:name', function($name){
    echo "Hello $name !!!";
})
->execute();
</code>

如果客户不设置伪静态,默认URL为第2种形式.

如果客户设置使用伪静态,就分别生成Apache和Nginx的rewrite规则,让客户自已复制去配置.
规则可以这样..

<code class="lang-apache">#Apache .htaccess
RewriteEngine on 
RewriteBase / 
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule . /index.php [L,QSA]
</code>

这样,程序只需按照第2种的形式处理$_SERVER['REQUEST_URI'].程序中的URL根据是否设置伪静态,来动态生成.

所有请求(除静态文件请求和不存在的目录或文件)外,全部转发到index.php。

Dispatcher中先进行自定义Router检测,匹配Router则执行Router绑定操作。未匹配到Router规则,执行常规操作检测并执行。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn