thinkphp url和路由

一.入口模块修改 

修改public下的index 加入 define('BIND_MODULE','admin'); 即可将入门模块绑定到admin模块

<?php

// [ 应用入口文件 ]

// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');

//定义配置文件目录
define('CONF_PATH',__DIR__.'/../conf/');
define('BIND_MODULE','admin');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';

二.路由美化  在conf 目录创建route.php

1.开启路由配置

// 是否开启路由
    'url_route_on'           => true,
// 是否强制使用路由
'url_route_must'         => false,

2.在conf目录新建 route.php

<?php

return [
    'new/:id' => 'admin/index/index',
];

3.助手函数 url()直接修改url

  public function index($id){
        echo url('admin/index/index',['id'=>11]).'<br />';
        return "index";
    }

4.route.php 路由规则

'news/' => 'admin/index/index',  // 添加路由规则 路由到 index控制器的index操作方法
http://localhost/index
'news/:id' => 'admin/index/index', //该路由规则表示所有index开头的并且带参数的访问都会路由到index控制器的index操作方法,且参数不能为空
http://localhost/index?id=6&name=dd
'news/[:id]' => 'admin/index/index',  // 路由参数name为可选
http://localhost/index?id=7 或 http://localhost/index 均可
 

 动态路由 

/* 动态路由设置 */
use 	hinkRoute;
/* 将Index控制器设置别名为斜杠 /  */
Route::alias('test','appindexcontrollerTest');

/* 设置路由规则 */
Route::rule([
    'news/:id'  =>  'index/test/index',
    'blog/:id' =>  ['Blog/update',['ext'=>'html'],['id'=>'d{4}']],],'','GET',['ext'=>'html'],['id'=>'d+']);

静态路由 在conf目录route文件配置

<?php
return
  # ext 为url后缀
  ['news/:name' => 'index/test/test',['ext'=>'html']]
;

apache2 配置文件在public .htaccess

 /* vim /etc/apache2/apache2.conf  */

<IfModule mod_rewrite.c>
 RewriteEngine on

     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_URI} !(.html)$ [NC]
     RewriteRule ^/?(.*)$ /$1.html [QSA,R=301,L]

     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

#//第二种方法
#RewriteEngine on
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>

浏览器访问时 在phpstorm调试时要加index.php 例如下面的url要写成  https/localhost/index.php/news/6.html 不然无法访问。

生产环境不用加index.php

 

以下可以不用设置

1.修改apache2服务器的httpd.conf

var/www# vim /usr/local/apache2/conf/httpd.conf

确认加载了 mod_rewrite.so 模块(将如下配置前的 # 号去掉):

LoadModule rewrite_module modules/mod_rewrite.so

更改AllowOverride 配置

<Directory /var/www/tp5/public>
MultiViews
    Options Indexes FollowSymLinks
    AllowOverride FileInfo Options
    #AllowOverride None

    Require all granted
</Directory>
原文地址:https://www.cnblogs.com/jiangfeilong/p/11198747.html