V
V
Vatrush2019-05-04 17:25:32
Laravel
Vatrush, 2019-05-04 17:25:32

How to render a view along the same route depending on a condition?

Here is my router code

Route::group(['middleware' => 'guest'], function () {
    Route::get('/', function () {
        return view('layouts/login');
    })->name('login');
});

Route::group(['middleware' => 'auth'], function () {
    Route::get('/', function () {
        return view('index');
    })->name('index');
    Route::get('/', function () {
        return view('layouts/main');
    })->name('main');
    Route::get('/profile', function () {
        return view('layouts/profile');
    })->name('profile');
    Route::get('/exchange', function () {
        return view('layouts/exchange');
    })->name('exchange');
});

If I use in the guest group instead of the route "/" any other "login",'"auth" and so on. then everything is fine, but if you leave "/", then it will crash. It is Route [login] not defined.
important to use the main page for login.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Aksentiev, 2019-05-04
@Vatrush

First, learn how to create controllers.
Then read about Auth and how to find out if the user is currently logged in or not, depending on this, give different templates, and do not create "2 routes per link" because. it's impossible.

N
NubasLol, 2019-05-04
@NubasLol

if (auth()->guest()) {
Route::group(['middleware' => 'guest'], function () {
    Route::get('/', function () {
        return view('layouts/login');
    })->name('login');
});
} else {

Route::group(['middleware' => 'auth'], function () {
    Route::get('/', function () {
        return view('index');
    })->name('index');
    Route::get('/', function () {
        return view('layouts/main');
    })->name('main');
    Route::get('/profile', function () {
        return view('layouts/profile');
    })->name('profile');
    Route::get('/exchange', function () {
        return view('layouts/exchange');
    })->name('exchange');
});
}

D
Dmitry, 2019-05-06
@Astatroth

Why can't you take authentication out of the box? You can even top it off if you need to. Why be so perverted?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question