编程语言
首页 > 编程语言> > thinkphp6: 开启强制路由(php 8.1.1 / thinkphp v6.0.10LTS )

thinkphp6: 开启强制路由(php 8.1.1 / thinkphp v6.0.10LTS )

作者:互联网

一,thinkphp6配置强制路由:

1,修改config/route.php 设置url_route_must一项的值为true 如下:
     // 是否强制使用路由
    //'url_route_must'          => false,
    'url_route_must'        => true,
2,查看修改后的效果: 访问:
http://192.168.219.6:8000/article/onemedia?id=1
返回: 3,配置路由: 修改route/app.php 增加一行:
Route::get('article/onemedia','article/oneMedia’);
如下:
<?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>
// +----------------------------------------------------------------------
use think\facade\Route;

Route::get('article/onemedia','article/oneMedia’);
4,测试效果: 访问:
http://192.168.219.6:8000/article/onemedia?id=1
返回:

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,,配置路由分组:

1,修改route/app.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>
// +----------------------------------------------------------------------
use think\facade\Route;
Route::group('article', function () {
    Route::get('onemedia', 'article/oneMedia');
    Route::get('qr', 'article/qr');
});
2,测试效果: 此时qr也可以访问了

三,路由规则不匹配时的处理:

1,在route/app.php中增加一条:
Route::miss('article/miss’);
2,article.php
<?php
declare (strict_types = 1);
 
namespace app\controller;
 
use app\result\Result;
 
class Article extends BaseController
{
    //用来匹配无效的路由
    public function miss() {
        return  Result::Error(404,"路由不匹配");
    }
}
3,测试效果: 访问:
http://192.168.219.6:8000/sogou/china
返回:

四,查看php和thinkphp的版本: 

php:
liuhongdi@lhdpc:/data/php/admapi$ php --version
PHP 8.1.1 (cli) (built: Dec 20 2021 16:12:16) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies 
thinkphp:
liuhongdi@lhdpc:/var/www/html$ cd /data/php/admapi/
liuhongdi@lhdpc:/data/php/admapi$ php think version
v6.0.10LTS 

 

标签:8.1,v6.0,10LTS,liuhongdi,Route,article,php,route,路由
来源: https://www.cnblogs.com/architectforest/p/15806306.html