T
T
Taras Parashchuk2020-11-30 21:35:48
Laravel
Taras Parashchuk, 2020-11-30 21:35:48

How to redirect to different route from web.php file in laravel?

Hello.
I have a variable in the session based on the value of which I want to determine which route to call and where to redirect the user. For example, a variable from car brands.

Route::get('/', function (Request $request) {
      $brand = $request->session()->get('brand');           // Пользователь попадает на домашнюю если установлена "brand"
    if ($brand = "VAG") {  redirect to vag controller  }      //  перенаправить на vag
    if ($brand = "BMW") {  redirect to bmv controller  }   //  перенаправить на bmv
    if ($brand = "VOLVO") {  redirect to volvo controller } //  перенаправить на volvo
    return view('layouts.layout');    });  // если переменная не определена то вернуть страницу по умолчанию.


Below are the routes of each brand
Route::get('/bmw','[email protected]');
Route::get('/vag', '[email protected]');
Route::get('/volvo', '[email protected]');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Smolyakov, 2020-11-30
@taras1978

It's pretty simple:

Route::get('/', function () {
    return redirect('/bmw');
});

More documentation here: https://laravel.com/docs/8.x/redirects

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question