简体   繁体   中英

Redirect user to same page after login in a specific page

I have this route:

Route::group(['prefix' => '', 'middleware' => 'auth'], function(){
    Route::get('/congress/create', [
        'uses' => 'CongressController@create',
        'as'   => 'congress.create'
    ]);
  }

When user acessses this url:

<li><a href="{!! route('congress.create') !!}"> Create Congress </a></li>

The user goes to " http://project.test/login ", because the user needs to be authenticated to access the congressc reate page. When the user is authenticated he is redirected to the " http://project.test/congress/create ". So this part is working fine.

Issue:

But then I have a link so the user can access the congress details page:

<a href="{{route('congress.show', ['id' => $congress->id, 'slug' => $congress->slug])}}">More congress details</a>

The user can access the congress details page " http://project.test/congress/1/congress-test " without being authenticated. The route is:

Route::get('/congress/{id}/{slug?}', [
    'uses' => 'FrontController@show',
    'as'   =>'congresses.show'
]);

Then in the congress details page there is a form with this route:

 <form method="post" action="{{route('congresses.registration', ['id' => $congress->id, 'slug' => $congress->slug])}}">

Route for this form:

Route::post('/congress/{id}/{slug?}/registration', [
    'uses' => 'RegistrationController@storeQuantity',
    'as'   =>'congresses.registration'
]);

In the storeQuantity method are stored the selected quantities by the user for each congress ticket type and then the user is redirected to the registration page:

return view('congresses.registration')->with('selectedRtypes', $selectedRtypes) ;

So when the user submits the form he goes to " http://project.test/event/1/congress-test/registration ". In this page if the user is not authenticated it appears this message:

@if(!\Auth::check())
    <div class="alert alert-info" role="alert">
        <span>
            Already have an account? 
            <a data-toggle="modal" 
            data-target=".bd-example-modal-lg" 
            href="">Login.</a>
        </span>
    </div>
@endif

If user clicks in Login, it appears a modal so the user can insert the email and password and if login with success the modal should close and he should be redirected to the same registration page.

Issue : The issue is that if the user logins with success in this page " http://project.test/congress/1/congress-test/registration " with the form in the modal he is redirected to the "Create congress" page (" http://project.test/congress/create ") instead of staying in the registration page (" http://project.test/congress/1/congress-test/registration "). Do you know how to correct this issue?

LoginController:

class LoginController extends Controller
{

    use AuthenticatesUsers;


    protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    protected function authenticated(Request $request, $user)
    {

        return redirect()->intended($this->redirectTo);

    }
}

Example of the context: 在此处输入图片说明

Put the route Route::post('/congress/{id}/{slug?}/registration') above Route::get('/congress/{id}/{slug?}')

As you log in with a modal, the underlying page stays 'static'. It may hold 'old' information. What if you add a condition to the authenticated function where you check if the route is equal to 'congresses.registration' and set $redirectTo this page manually.

As a side note, it is better not to add anything after conditions ({slug?}) unless the slug is always there. Then you can as well drop the '?'. I know it's a GET and a POST so there is probably no confusion. Make sure your slug can not be the word 'registration' by adding it the the $reserved array in config/sluggable.php .

Try this..

In your authenticated method, check the previous request url is equal to route('congresses.registration'). If yes redirect to route('congresses.registration') with parameters.

LoginController:

use Illuminate\Support\Facades\Route;

class LoginController extends Controller
{

    use AuthenticatesUsers;


    protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    protected function authenticated(Request $request, $user)
    {
        //check if the previous page route name is 'congresses.registration'
        if(Route::getRoutes()->match(Request::create(\URL::previous()))->getName() == 'congresses.registration') {
            //redirect to previous page with parameters
            return redirect(Request::create(\URL::previous())->getRequestUri());
        }
        return redirect()->intended($this->redirectTo);

    }
}

Hope it helps.. Let me know the results..

Reference here

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