luck-draw/route/app.php

77 lines
2.7 KiB
PHP

<?php
use think\facade\Route;
use think\Request;
use app\model\ArchivesCategory ;
use think\facade\Cache;
//走遍所有栏目 缓存起来
Route::get('archive/:articleId', "page/archivesInfo")->pattern(['articleId' => '\d+']); //articleId 只匹配非负证书数;
Route::get('page/:categoryId', "page/index")->pattern(['articleId' => '\d+']); //articleId 只匹配非负证书数;
if(Cache::has("categoryNames")){
$categoryNames = Cache::get("categoryNames");
}else{
$categoryNames = ArchivesCategory::getRoute();
Cache::set("categoryNames",$categoryNames,86400);
}
foreach ($categoryNames as $item) {
//路由别名入口
if(!empty($item['route'])){
Route::get($item['route']."$", "page/index")->append(['categoryId'=>$item['id']]);
}
}
foreach ($categoryNames as $item) {
if(!empty($item['route'])){
//文章的
Route::get($item['route'].'/:articleId$', "page/archivesInfo")
->pattern(['articleId' => '\d+']); //articleId 只匹配非负整数
}
}
// 验证码
Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@index");
// 单应用模式下 多模块间兼容 模块/控制器/操作的访问模式 等同于 模块.控制器/操作
Route::group(function (Request $request) {
// 控制器必需采用大驼峰,方法必需采用小驼峰
$baseUrl = $request->baseUrl();
$baseUrl = explode('.htm', $baseUrl)[0];
$urlArr = explode('/', ltrim($baseUrl, '/'));
if (!strpos($baseUrl, '.')) {
$length = count($urlArr);
$dirList = [];
if ($length >= 4) {
$module = $urlArr[0];
$dirList = array_slice($urlArr, 1, $length - 3);
$controller = $urlArr[$length - 2] ?? 'index';
$action = $urlArr[$length - 1] ?? 'index';
} else {
$module = '';
$controller = $urlArr[0] ?? 'index';
$action = $urlArr[1] ?? 'index';
if (in_array($urlArr[0], ['api', 'manager'])) {
$module = $urlArr[0];
$controller = $urlArr[1] ?? 'index';
$action = $urlArr[2] ?? 'index';
}
}
$dir = '';
if (!empty($dirList)) {
foreach ($dirList as $d) {
$dir .= toCamelString($d, true).'.';
}
}
// 模块(支持多级目录).控制器/操作 如:/api/aa-bb-cc/dd-ee-ff/gg-hh-ii/controller/action
// 将访问 api.aaBbCc.ddEeFf.ggHhIi.controller/action
$moduleSeparate = !empty($module) ? '.' : '';
$route = $module.$moduleSeparate.$dir.toCamelString($controller).'/'.toCamelString($action, true);
Route::rule($baseUrl, $route);
}
});