简体   繁体   English

Routes.php Laravel中的条件

[英]Conditions In Routes.php Laravel

Sorry guys i am new in Laravel. 抱歉,我是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 我想使用laravel 5.2在Routes.php中使用条件。登录用户后,我想调用showExperience函数,否则重定向到登录。

My routes.php code 我的routes.php代码

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

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

here is My ProfilesController.php 这是我的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 然后,您可以转到app/Http/Middleware/Authenticate.phpapp/Http/Middleware/RedirectIfAuthenticated.php来更改默认重定向值

Here is My Solution 这是我的解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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