我在 Laravel 上有一个包含大量页面的项目。我想让我的页面之一即使对于未经授权的用户也可用。这就像“登录”页面。例如,让每个人都可以自由地看到“mysite.com/hello”页面的内容(这不是“登录”页面,但每个拥有此链接的人都可以看到该页面的内容)。 您能告诉我如何使用路由/ Controller 来在未经授权的情况下显示此页面吗?
最佳答案
在你的web.php
Route::get('/hello','MyController@hello');
Route::get('/hello2','MyController@hello2');
Route::get('/hi','MyController@hi');
在你的MyController.php
class MyController extends Controller
{
public function __construct()
{
$this->middleware('auth',['except'=>['hello','hello2']]);
}
// this function do not use auth middleware
public function hello(){
return response()->json(['hello'=>'hello page for all']);
}
public function hello2(){
return response()->json(['hello2'=>'hello2 page for all']);
}
// this function use auth middleware
public function hi(){
// check if user is logged in ?
if(Auth::check()){
return response()->json(['hi'=>'hi page for logged user in only']);
}else{
// user is not logged in
return response()->json(['hi'=>'please login to see content']);
// redirect back to login
// return redirect('/login');
}
}
}
关于php - Laravel:未经授权即可使页面可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53482770/