简体   繁体   中英

Laravel 5 Login redirect to a subdomain

For my web application we have sub-domains for each account account1.example.org & account2.example.org . They all go to the same place just it allows us to have the account in the URL.

I have everything working with the sub-domains. My issue is with the Laravel 5 login code. 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).

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::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.

In my AuthController.php I added

/**
 * 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. 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.

To make sure that the login worked I also had to change the domain item in config/session.php file. 'domain' => null -> 'domain' => '.example.org' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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