Laravel认证的动态子域路由 [英] Laravel authenticated dynamic subdomain routing

查看:62
本文介绍了Laravel认证的动态子域路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使经过身份验证的子域路由适用于某些特定的可变子域:

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

这些应由auth中间件保护.它们基本上都引用了 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...

创建了此中间件,以防止subdomain参数弄乱其他路由,并允许成功的身份验证重定向到 app.example.com :

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

将此添加到Kernel.php:

Added this to Kernel.php:

protected $routeMiddleware = [
    'subdomain' => \App\Http\Middleware\Subdomain::class,
];

routes.php的内容:

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

当我访问任何路由时,我都可以跟踪到没有任何东西可以到达 subdomain 中间件……它只是路由到404页面.

When I access any route, I can trace that nothing hits the subdomain middleware...it just routes to the 404 page.

我如何在Laravel 5.2中进行这项工作?

How would I make this work in Laravel 5.2?

推荐答案

由于我的设置目标是允许处理带有可选环境前缀的某些子域组,因此我以以下方式进行处理.

Since the goal of my setup was to allow handling certain subdomain groups with optional environment prefixes, I handled it in the following way.

我不必要地放弃了 Subdomain 类.

我将此添加到了 .env 文件中,以便每个环境都可以拥有自己的域,因此本地开发服务器仍然可以独立于暂存和生产服务器运行:

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

config/app.php 内,我添加了:

'domain' => env('APP_DOMAIN', null),

我将这些方法添加到 \ 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 : '');
}

Auth \ AuthController.php 内,我添加了此代码来处理从 example.com app.example.com 的重定向,即使有前缀使用 staging testing :

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

routes.php的新内容:

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!

这篇关于Laravel认证的动态子域路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆