TP5多入口配置与Nginx去后缀优化
在使用TP5进行项目开发时,前台默认绑定index模块较为方便,但后台模块的独立配置起初并不直观。查阅手册后,按照指引将index.php复制并重命名为admin.php,以此作为后台专用的入口文件。此时,通过域名/admin.php即可访问后台,系统默认指向admin模块下index控制器的index方法。尽管功能已实现,但对于追求完美的开发者而言,URL中残留的.php后缀显得不够美观。为了优化这一细节,尝试调整Nginx服务器配置,最终效果令人满意,成功去除了后缀,使访问路径更加简洁清爽。具体配置代码如下所示。
入口文件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/');
// 绑定到admin模块
define('BIND_MODULE','admin');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
?>
后台首页Index.php
<?php
/*
*功能:后台首页控制器
*作者:魏安来
*日期:2017/12/12
*/
namespace app\admin\controller;
class Index extends Base{
/*后台首页*/
public function index(){
return 'admin';
//return $this->fetch();
}
}
?>
nginx配置vhosts.conf
server {
listen 80;
server_name www.tpmall.com tpmall.com;
root "F:/phpStudy/WWW/tpmall/public";
location / {
index index.html index.htm index.php admin.php;
#autoindex on;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=/$1 last;
}
if (!-e $request_filename){
rewrite ^(.*)$ /admin.php?s=/$1 last;
}
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
至此,关于TP5多入口设置及去除后缀的实例讲解已完成。如需了解更多相关TP5内容,建议搜索本站历史文章或继续阅读后续推荐内容。感谢大家的支持!