Files
swiftadmin/config/route.php

95 lines
2.9 KiB
PHP
Raw Normal View History

2022-08-19 19:48:37 +08:00
<?php
// +----------------------------------------------------------------------
// | swiftAdmin 极速开发框架 [基于 WebMan 开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2022-2030 http://www.swiftadmin.net
// +----------------------------------------------------------------------
// | swiftAdmin.net High Speed Development Framework
// +----------------------------------------------------------------------
// | Author: meystack <coolsec@foxmail.com> Apache 2.0 License
// +----------------------------------------------------------------------
use Webman\Route;
2022-11-28 19:11:12 +08:00
// 验证码路由
Route::any('/captcha', [app\BaseController::class, 'captcha']);
/**
* 默认管理员路由
* @var string $manage
*/
2022-08-19 19:48:37 +08:00
Route::any('/manage', function () {
2022-08-22 23:04:57 +08:00
request()->session()->set('AdminLogin', ['_security' => request()->buildToken()]);
2022-08-22 19:07:32 +08:00
return redirect('/admin/login');
2022-08-19 19:48:37 +08:00
});
2022-11-28 19:11:12 +08:00
/**
* 加载插件全局初始化路由
* @var array $pluginRoute
*/
Route::any('/static/system/js/plugin.js', function () {
$plugins = get_plugin_list();
$pluginFiles = [];
foreach ($plugins as $item) {
if (!$item['status']) {
2022-08-19 19:48:37 +08:00
continue;
}
2022-11-28 19:11:12 +08:00
// 是否存在javascript文件
$pluginJs = plugin_path($item['name']) . 'plugin.js';
if (file_exists($pluginJs)) {
$pluginFiles[] = read_file($pluginJs);
2022-08-19 19:48:37 +08:00
}
}
2022-11-28 19:11:12 +08:00
return $pluginFiles ? implode(PHP_EOL, $pluginFiles) : '';
});
2022-08-19 19:48:37 +08:00
2022-11-28 19:11:12 +08:00
/**
* 全局404路由fallback
* @var array $request
* @var array $response
*/
2022-08-19 19:48:37 +08:00
Route::fallback(function ($request) {
2023-04-25 20:11:49 +08:00
2022-08-19 19:48:37 +08:00
$pathInfo = parse_url(request()->url());
if (!isset($pathInfo['path'])) {
$parseApp = ['index'];
2023-04-25 20:11:49 +08:00
} elseif ($pathInfo['path'] == '/') {
// 默认应用路由
return redirect('/index');
2022-08-19 19:48:37 +08:00
} else {
$parseApp = explode('/', ltrim($pathInfo['path'], '/'));
}
2023-04-25 20:11:49 +08:00
// 判断是否为API接口
2022-08-19 19:48:37 +08:00
if ($request->expectsJson()) {
return json(['code' => 404, 'msg' => '404 not found']);
}
2023-04-25 20:11:49 +08:00
2022-08-19 19:48:37 +08:00
return response(request_error(current($parseApp)), 404);
2023-06-19 14:32:30 +08:00
});
// 执行插件初始化
$pluginList = get_plugin_list();
foreach ($pluginList as $item) {
if (!$item['status']) {
continue;
}
foreach ($item['rewrite'] as $route => $value) {
$separator = explode('/', $value);
$method = end($separator);
array_pop($separator);
$filePath = implode('\\', $separator);
$controller = 'app\\index\\controller\\' . $filePath;
if (class_exists($controller) && method_exists($controller, $method)) {
try {
$classFullName = (new ReflectionClass($controller))->getName();
Route::any($route, [$classFullName, $method]);
} catch (\Throwable $e) {
var_dump('error: ' . $e->getMessage());
}
} else {
var_dump("\033[31m$controller or $method does not exist!\033[0m");
}
}
}