简体   繁体   English

Laravel 5登录重定向到子域

[英]Laravel 5 Login redirect to a subdomain

For my web application we have sub-domains for each account account1.example.org & account2.example.org . 对于我的Web应用程序,每个帐户都有一个子域account1.example.org & account2.example.org They all go to the same place just it allows us to have the account in the URL. 他们都去了同一个地方,只是它允许我们在URL中拥有该帐户。

I have everything working with the sub-domains. 我可以使用所有子域。 My issue is with the Laravel 5 login code. 我的问题是Laravel 5登录代码。 I am unsure on how to get it to redirect to the sub-domain (which is stored in the DB and accessible by Auth::user()->account->sub_domain). 我不确定如何重定向到子域(存储在数据库中,并且可以通过Auth :: user()-> account-> sub_domain访问)。

I have seen people try and change the action but not add to the address. 我见过人们尝试更改操作,但未添加到地址中。 What is the best way to do this? 做这个的最好方式是什么?

Define route - 定义route -

Route::get(['domain' => '{account}.myapp.com'], function($account){
    // Process data or redirect
});

And after login - 登录后-

Redirect::to('account1.myapp.com');

So with the help from sgt BOSE, this is what I came up with. 因此,在sgt BOSE的帮助下,这就是我想到的。

In my AuthController.php I added 在我的AuthController.php我添加了

/**
 * Redirect Path
 *
 * @return \Illuminate\Http\RedirectResponse
 */
public function redirectPath()
{
    $redirectTo = property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';

    $url = Config::get('app.url');
    $subDomain = Auth::user()->account->sub_domain;
    $scheme = parse_url($url, PHP_URL_SCHEME);
    $host = parse_url($url, PHP_URL_HOST);
    return $scheme.'://'.$subDomain.'.'.$host.$redirectTo;
}

What this does is returns the link for the postLogin and postRegister. 这是返回postLogin和postRegister的链接。 This method is only called when they are successful. 仅在成功时才调用此方法。 The gets my sub-domain and builds a link based on the config item for my URL. 获取我的子域,并基于URL的配置项构建链接。

To make sure that the login worked I also had to change the domain item in config/session.php file. 为了确保登录有效,我还必须更改config/session.php文件中的域项目。 'domain' => null -> 'domain' => '.example.org' . 'domain' => null -> 'domain' => '.example.org'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM