使用 'if' 语句进行路由

Routing with 'if' statement

我需要我的网站位于登录墙后面。到目前为止,我已经用 "if" 语句完成了此操作:

Route::get('/', function() {
    if (Auth::check()) {
        return view('pages.feed');
    } else {
        return view('auth.login');
    }
});

不过我还需要调用 feedController。我怎样才能添加

'feedController@index'

要声明?

在控制器中添加您的条件而不是在 web.php 路由中

Route::get('/', 'feedController@index');

并在 feedController 控制器中

function index(){
    .........
    if (Auth::check()) {
        return view('pages.feed');
    } else {
        return view('auth.login');
    }
}

另外一定要使用use Illuminate\Support\Facades\Auth; 或者只是 \ 作为 if (\Auth::check()) {