我試圖得到驗證的子域路由對於一些特定變量子站點:Laravel認證的動態路由子域
app.example.com
staging.app.example.com
testing.app.example.com
這些應該由AUTH中間件把守。它們基本上都參考app.example.com
,但對於不同的環境。擊中這些領域
一切都應該去來賓路線:
example.com
staging.example.com
testing.example.com
這是我到目前爲止已經試過......
創造了這個中間件,以防止子域參數從搞亂其他路線,並允許成功驗證重定向到app.example.com
:
class Subdomain
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$route = $request->route();
$subdomain = $route->parameter('subdomain');
if (!empty($subdomain) && preg_match('/^(staging|testing)\.(app.\)?/', $subdomain, $m)) {
\Session::put('subdomain', $m[1]);
}
$route->forgetParameter('subdomain');
return $next($request);
}
}
將此添加到Kernel.php:
protected $routeMiddleware = [
'subdomain' => \App\Http\Middleware\Subdomain::class,
];
routes.php文件的內容:
Route::group(['domain' => '{subdomain?}example.com', 'middleware' => 'subdomain'], function() {
// Backend routes
Route::group(['middleware' => 'auth'], function() {
Route::get('/', ['as' => 'dashboard', 'uses' => '[email protected]']);
// ...various other backend routes...
});
// Frontend routes
Route::auth();
Route::get('/', function() {
return view('frontend');
});
});
當我訪問任何途徑,我可以跟蹤沒有命中subdomain
中間件...它只是路由到404頁。
我該如何在Laravel 5.2中完成這項工作?