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 \think\Route;
/* 將Index控制器設置別名為斜杠 /  */
Route::alias('test','\app\index\controller\Test');

/* 設置路由規則 */
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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM