8.2 tp5 入口文件和路由
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');