问题
I'm trying to get authenticated subdomain routing working for some specific variable subdomains:
app.example.com
staging.app.example.com
testing.app.example.com
These should be guarded by the auth middleware. They all essentially reference app.example.com
but for different environments.
Everything that hits these domains should go to the guest routes:
example.com
staging.example.com
testing.example.com
This is what I've tried so far...
Created this middleware to prevent the subdomain parameter from messing up other routes and to allow successful authentication to redirect to 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);
}
}
Added this to Kernel.php:
protected $routeMiddleware = [
'subdomain' => \App\Http\Middleware\Subdomain::class,
];
Contents of routes.php:
Route::group(['domain' => '{subdomain?}example.com', 'middleware' => 'subdomain'], function () {
// Backend routes
Route::group(['middleware' => 'auth'], function () {
Route::get('/', ['as' => 'dashboard', 'uses' => 'Controller@dashboard']);
// ...various other backend routes...
});
// Frontend routes
Route::auth();
Route::get('/', function () {
return view('frontend');
});
});
When I access any route, I can trace that nothing hits the subdomain
middleware...it just routes to the 404 page.
How would I make this work in Laravel 5.2?
回答1:
Since the goal of my setup was to allow handling certain subdomain groups with optional environment prefixes, I handled it in the following way.
I dropped the Subdomain
class as being unnecessary.
I added this to the .env
file so that each environment can have it's own domain so the local dev server still works independent of the staging and production server:
APP_DOMAIN=example.dev
On production and staging it would simply be:
APP_DOMAIN=example.com
Within config/app.php
I added:
'domain' => env('APP_DOMAIN', null),
I added these methods to \App\Http\Controllers\Controller
:
public static function getAppDomain()
{
return (!in_array(\App::environment(), ['local', 'production']) ? \App::environment() . '.' : '') . 'app.' . config('app.domain');
}
public static function getAppUrl($path = '', $secure = false)
{
return ($secure ? 'https' : 'http') . '://' . static::getAppDomain() . ($path ? '/' . $path : '');
}
Within Auth\AuthController.php
I added this to handle redirects to the app.example.com
from example.com
even if prefixed with staging
or testing
:
public function redirectPath()
{
if (\Auth::check()) {
return redirect()->intended(static::getAppUrl())->getTargetUrl();
} else {
return $this->redirectTo;
}
}
New contents of routes.php:
// Backend routes
Route::group(['domain' => Controller::getAppDomain(), 'middleware' => 'auth'], function () {
Route::get('/', ['as' => 'dashboard', 'uses' => 'Controller@dashboard']);
// ...various other backend routes...
});
// Frontend routes
Route::auth();
Route::get('/', function () {
return view('frontend');
});
Hope this helps if anyone tries similar!
来源:https://stackoverflow.com/questions/39605467/laravel-authenticated-dynamic-subdomain-routing