【问题标题】:Conditions In Routes.php LaravelRoutes.php Laravel 中的条件
【发布时间】:2016-09-23 09:13:18
【问题描述】:

对不起,我是 Laravel 的新手。 我想在 Routes.php 中使用 laravel 5.2 中的条件 当用户登录时,我想调用 showExperience 函数,否则重定向到登录

我的 routes.php 代码

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

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

这是我的 ProfilesController.php

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

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

【问题讨论】:

    标签: php laravel-5 laravel-routing


    【解决方案1】:

    您应该为此使用middleware

    例如:

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

    然后你可以去app/Http/Middleware/Authenticate.phpapp/Http/Middleware/RedirectIfAuthenticated.php更改默认重定向值

    【讨论】:

    • 谢谢@Mister M。我得到的答案和你的答案完全一样。
    • 没问题。另外,请考虑接受我的回答。
    • 我正在尝试,但它说您的声誉不超过 15 以公开展示您的投票
    【解决方案2】:

    这是我的解决方案

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

    【讨论】:

      猜你喜欢
      • 2017-01-11
      • 2015-07-19
      • 2016-11-23
      • 2016-01-16
      • 2014-06-08
      • 2016-02-16
      • 2016-06-20
      • 1970-01-01
      • 2014-03-03
      相关资源
      最近更新 更多