当前位置:首页 > 后端 > 正文内容

ThinkPHP6多应用下配置短路由 - TP6路由

Z先生4年前 (2022-03-13)后端745

需要实现的效果:

http://xx.com/u/RkdJ80      =>     http://xx.com/home/url/url/index

实现步骤

1. 设置tp6隐藏网址的index.php

具体参考官方文档:https://www.kancloud.cn/manual/thinkphp6_0/1037488

2. 然后 config\app.php 设置,开启路由,设置默认路由

    'with_route'       => true,
    'default_app'      => 'home',

3. 写好请求方法,确定正常能访问到

子目录模式:app\home\controller\url\UrlController.php文件下,index方法,确定完整地址能访问:http://xx.com/home/url/url/index

4. 设置应用下路由

设置home应用下的路由定义app\home\route\route.php(目录名固定,文件名随意)下:

<?php

use think\facade\Route;

Route::get('/', 'index.Index/index');

// TODO 短网址实现
// http://demo.demo/u/Udos81d

Route::get('u/:hash',       'url.url/index');
Route::get('u',       'url.url/index');


// controller 二级目录,动态路由

Route::get(':dir/:class/:fun', ':dir.:class/:fun');

Route::post(':dir/:class/:fun', ':dir.:class/:fun');

根据实际情况修改:hash:hash为方法中param取值的key

5. 修改入口文件

原入口文件:public/index.php,内容为

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

// [ 应用入口文件 ]
namespace think;

require __DIR__ . '/../vendor/autoload.php';

// 执行HTTP应用并响应
$http = (new App())->http;

$response = $http->run();

$response->send();

$http->end($response);

修改为:

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

// [ 应用入口文件 ]
namespace think;

require __DIR__ . '/../vendor/autoload.php';

// 执行HTTP应用并响应
$http = (new App())->http;


// 短网址修改by OneNine
$uri = trim($_SERVER['REQUEST_URI'], '/');

$uri_arr = explode('/', $uri);

$default_app = "home";


// TODO 新加入应用,需要在这里再次标识
$other_app = ['adm', 'payment'];

if (in_array($uri_arr[0], $other_app)) {
    $response = $http->run();
} else {
    $response = $http->name($default_app)->run();
}

$response->send();

$http->end($response);

注意:
根据实际情况修改以上:$default_app$other_app里面的值,最终效果如下:

分享给朋友:

相关文章

php json_decode转换为null的问题解决

在做某一程序的时候,前台组装的json数组,为了能传到后台,使用JSON.stringify将其转换成字符串后后端接收。但是接收后,死活转换不了数组,使用json_last_error()输出错误后,...

【转】rtsp视频流实现浏览器h5播放

【转】rtsp视频流实现浏览器h5播放

方案思路:获取摄像头数据流 --> FFmpeg转码 --> Node.js(搭建webSocket服务器) --> 在服务器上运行jsmpeg程序操作步骤首先查看是否能够正常地获取...

php array_column函数封装

array_column函数只支持php5.5.0以上版本,所以这里要封装下,这样可以兼容用法if (!function_exists("array_column"))&...

Composer的初步使用以及Composer切换国内源(阿里云)

Composer安装(Windows系统)下载:https://getcomposer.org/Composer-Setup.exe一步步安装即可。需要注意的是你需要开启 openssl 配置,我们打...

困扰了很久的PHP跨域问题

其实这个问题一直是不存在的,只是自己因为各种配置吧,导致了这个问题一直没有解决。现在就解决思路提供如下: 需要两步操作一、修改Nignx配置文件一般在宝塔面板伪静态中设置示例代码: locatio...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。
请先 登录 再评论,若不是会员请先 注册