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; } } }