http://xx.com/u/RkdJ80 => http://xx.com/home/url/url/index
具体参考官方文档:www.kancloud.cn/manual/thin…
'with_route' => true, 'default_app' => 'home',
子目录模式:app\home\controller\url\UrlController.php文件下,index方法,
确定完整地址能访问:http://xx.com/home/url/url/index
设置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。
原入口文件:public/index.php,内容为
<?php namespace think; require __DIR__ . '/../vendor/autoload.php'; // 执行HTTP应用并响应 $http = (new App())->http; $response = $http->run(); $response->send(); $http->end($response);
修改为:
<?php
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里面的值,最终效果如下:
如果觉得博客文章对您有帮助,异或土豪有钱任性,可以通过以下扫码向我捐助。也可以动动手指,帮我分享和传播。您的肯定,是我不懈努力的动力!感谢各位亲~