几天前我开始使用laravel,我正面临这个问题:
永远不会返回NO
!
这是Controller
,你知道为什么吗?
Class TestController extends BaseController {
public function __construct()
{
if (!Auth::check()) return 'NO';
}
public function test($id)
{
return $id;
}
}
答案 0 :(得分:19)
<?php
class BaseController extends Controller {
public function __construct()
{
// Closure as callback
$this->beforeFilter(function(){
if(!Auth::check()) {
return 'no';
}
});
// or register filter name
// $this->beforeFilter('auth');
//
// and place this to app/filters.php
// Route::filter('auth', function()
// {
// if(!Auth::check()) {
// return 'no';
// }
// });
}
public function index()
{
return "I'm at index";
}
}
答案 1 :(得分:0)
对于 Laravel 5.x:
public function __construct()
{
$this->middleware(function(){
if (!Auth::check()) return 'NO';
});
}