用thinkphp5.1(tp5.1)做一个小网站,为了网站的安全,打算前后台采用不同域名,且开启强制路由模式。
前台是index模块,用www.index.com。
后台是admin模块,用www.admin.com。
操作方式如下:
1、在网站上绑定两个域名,www.index.com和www.admin.com
2、配置路由route/route.php,格式配置如下:
Route::rule('list','index/list')->ext('html');
Route::rule('detail/:id','index/detail')->ext('html');
Route::rule('order/:id','index/order')->ext('html');
Route::domain('www.admin.com',function(){
Route::rule('admin/auth/login','admin/auth/login')->ext('html');
Route::rule('admin/auth/captcha','admin/auth/captcha');
Route::rule('admin/admin_log/index','admin/admin_log/index')->ext('html');
Route::rule('admin/admin_log/view','admin/admin_log/view')->ext('html');
Route::rule('admin/admin_menu/add','admin/admin_menu/add')->ext('html');
Route::rule('admin/admin_menu/del','admin/admin_menu/del')->ext('html');
});
3、在config/app.php中设置'url_route_must' => true
在测试环境中访问正常,但升级到正式环境时,网页提示如下:
报错信息十分诡异,在runtime的log中只看到
“[ error ] [2]ini_set(): Headers already sent. You cannot change the session module's ini settings at this time”
详细看网页的提示,发现route.php怎么改,页面的Route::rule等报错信息都未改变。想到应该是缓存,于是把runtime中的cache和temp全部清空,再次操作,结果仍然是一样的。
这个事困扰了一天,终于在vi route.php时,注意到一个提示route.php有正在编辑中,有个.route.php.swp,心头一热,很可能是这货捣的鬼,于是把该文件删了,再重新访问,网页能正常。
swp文件是因为有一次在vi之后,非常规退出,导致产生了swp文件没有被自动删除。这次不知道为什么系统会去读这个swp文件,而未读取route.php文件,这个暂且不知。
在开发thinkphp5.1时经常会碰到各种各样的问题,如你在开发中有碰到一些问题,可以留言,我们一起交流。