18

几天前我从 laravel 开始,我正面临这个问题:

永远NO不会回来!

这是Controller,你知道为什么吗?

  Class TestController extends BaseController {

    public function __construct()
    {
        if (!Auth::check()) return 'NO';
    }

    public function test($id)
    {   
        return $id;
    }
}
4

2 回答 2

25
<?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";
    }
}
于 2013-07-17T11:03:19.300 回答
0

对于 laravel 5.x:

public function __construct()
{
    $this->middleware(function(){
        if (!Auth::check()) return 'NO';
    });        
}
于 2021-02-02T00:43:55.997 回答