我试图得到验证的子域路由对于一些特定变量子站点: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中完成这项工作?