duanbing6955 2018-04-23 11:46
浏览 77
已采纳

在Laravel中声明路由时在if语句中使用方法控制器

I want to use my show method in controller between if statement while declaring route:

Route::get('/leave', function() {
    if(Auth::user()->admin)
    {
        'uses' => 'TimeController@show'
    }
    else {
        return "not found";
    }
})->name('admin-time');

but the uses that I define it doesn't work! and I know it shouldn't works.

  • 写回答

2条回答 默认 最新

  • 普通网友 2018-04-23 11:58
    关注

    Route:

    Route::get('/leave', 'TimeController@show')->name('admin-time');
    

    Controller:

    public function show()
    {
        if(!Auth::user()->admin)
        {
            return abort(404);
        }
    
        // your code.
    }
    

    Remember: Do not use closure based routes. It prevents you from route:cache.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?