1、入口文件訪問優化
1) 在public文件夾下建立 admin.php文件
2) 打開admin.php文件,復制
// 定義應用目錄
define('APP_PATH', __DIR__ . '/../application/');
// 加載框架引導文件
require __DIR__ . '/../thinkphp/start.php';
3) 分別在兩個入口文件中綁定模塊
Public/index.php => define('BIND_MODULE', 'index');
Public/admin.php => define('BIND_MODULE','admin' );
之前的訪問
Index.php/index/Index/index admin.php/admin/Index/index
修改后的訪問(省略了模塊項)
Index.php/Index/index admin.php/Index/index
入口文件 控制器 方法
4)隱藏入口文件
Apache的配置過程,可以參考下:
a、httpd.conf配置文件中加載了mod_rewrite.so模塊
b、AllowOverride None 將None改為 All 在虛擬主機中把這一項改為All
c、在應用入口文件同級目錄添加.htaccess文件,內容如下:
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] 其中的index.php就是入口文件, 如果隱藏后台的入口文件 則改成admin.php
5 設置路由 動態單個注冊(TP5\thinkphp\library\think\Route.php)中的rule()方法
====================index.php==========================
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- // [ 應用入口文件 ] // 定義應用目錄 define('APP_PATH', __DIR__ . '/../application/'); define('BIND_MODULE', 'index'); // 加載框架引導文件 require __DIR__ . '/../thinkphp/start.php';
=========================admin.php===========================
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- // [ 應用入口文件 ] // 定義應用目錄 define('APP_PATH', __DIR__ . '/../application/'); define('BIND_MODULE','admin' ); // 加載框架引導文件 require __DIR__ . '/../thinkphp/start.php';
================.htaccess文件================
<IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] </IfModule>
================route.php=======================
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- /*return [ '__pattern__' => [ 'name' => '\w+', ], '[hello]' => [ ':id' => ['index/hello', ['method' => 'get'], ['id' => '\d+']], ':name' => ['index/hello', ['method' => 'post']], ], ];*/ use think\Route; //Route ::rule('index','index/index'); Route ::rule('test','Index1/test'); Route ::rule('get','Index1/getFunc');