简体   繁体   中英

Conditions In Routes.php Laravel

Sorry guys i am new in Laravel. I want to use conditions in Routes.php using laravel 5.2 When the User is logged i want to call the showExperience function else redirect to login

My routes.php code

Route::get('/profile', function () {
if (Auth::check()) {

    $url = action('ProfilesController@showExperience');
    return redirect($url);
} else {
    return Redirect::to('login');
}
});

here is My ProfilesController.php

public function showExperience(){
       $data = Experience::all();

    return view('profile')->with('experienceData',$data);
}

You shoud use middleware for this purpose

for example:

Route::get('/profile', ['middleware' => 'auth', 'uses' => 'YourController@index']);

Then you can go to app/Http/Middleware/Authenticate.php and app/Http/Middleware/RedirectIfAuthenticated.php to change default redirect values

Here is My Solution

Route::get('/profile',[
                        'uses' => 'ProfilesController@showExperience',
                        'as' => 'profile',
                        'middleware' => 'auth'
                              ]);

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