composer require qklin/laravel-auto-router
安装包里已包含了几个demo版本的Middleware,自取自用php
# add autorouter middleware $app->routeMiddleware([ //... 'check_sign' => \Qikl\AutoRouter\Middleware\CheckSignMiddleware::class, // ... more ]); # add provider $app->register(Qklin\AutoRouter\AppServiceProvider::class);
AUTO_ROUTER_MODULE_DIR=Modules LARAVEL_ORIGIN_HTTP_PREFIX=h AUTOROUTER_MODULE_HTTP_PREFIX=m,inside AUTOROUTER_DEFAULT_MIDDLEWARE=token AUTOROUTER_PROVIDER_START=1 AUTOROUTER_MIDDLEWARE_SUFFIX=OLNVIX # middleware AR_INSIDE_HOSTS=www.baidu.com,www.google.com AR_CHECK_SIGN_TIMEOUT=30 AR_CHECK_SIGN_KEY=sdfsdfdf AR_AUTH_API_DOMAIN=http://auth.baidu.com/checkauth AR_API_THROTTLE=ar_api_throttle # document variable AR_DOCUMENT_ROUTER=arRouter AR_DOCUMENT_METHOD=arMethod AR_DOCUMENT_ONLY_INSIDE=arOnlyInside
兼容原有已有在route.php里配置的路由laravel
默认取上面定义名称来讲明git
具体案例, 模块根目录和控制器文件:app/Modules/Module/Hotkeys/V1.0/Controllers/IndexController.php 方法:getList,注解arRouter getListO 路由:/m/module/hot-keys/v1_0/index/get-list-o /** * 案例一 纯路由注解 * 匹配:/m/module/articles/college/detail * @arRouter * @return string */ public function detail() { } /** * 案例二:路由注解并配置路由地址方法 * 匹配:/m/module/articles/college/detail-o * @arRouter detailO * @return string */ public function detail() { } /** * 案例三:路由注解请求方法和开启内网注解 * 匹配,且前缀必须inside开头:/inside/module/articles/college/detail-o * 只支持post和get请求方法 * @arRouter detailO * @arMehtod POST|GET * @arOnlyInside * @return string */ public function detail() { }
// 文件位置:app/config/route.php // 本文件可配置可不配置,根据需求配置 return [ "middleware" => [ //中间件,目前只支持 "controllers" => [ // 路由控制器路径 => 中间件 "m/module/ctl/action" => ["token", "validate"], ], "actions" => [ // 控制器方法 => 中间件 ] ] ];
需在Application能够注入configure方法api
/** * 加载配置文件 * @param $name */ public function configure($name) { if (isset($this->loadedConfigurations[$name])) { return; } $this->loadedConfigurations[$name] = true; $path = $this->getConfigurationPath($name); if ($path && is_file($path)) { $this->make('config')->set($name, require $path); } } /** * 获取配置路径 * @param null $name * @return string */ public function getConfigurationPath($name = null) { if (!$name) { $appConfigDir = $this->basePath('config') . '/'; if (file_exists($appConfigDir)) { return $appConfigDir; } else if (file_exists($path = __DIR__ . '/../config/')) { return $path; } } else { $appConfigPath = $this->basePath('config') . '/' . $name . '.php'; if (file_exists($appConfigPath)) { return $appConfigPath; } else if (file_exists($path = __DIR__ . '/../config/' . $name . '.php')) { return $path; } } }