Laravel 4:如何在过滤器内调用控制器?

时间:2013-08-08 16:52:39

标签: laravel laravel-4

我有这个类和过滤器:

class ApiController extends BaseController {

public function __construct()
{
    $this->afterFilter(function()
    {
        if ($this->response_type == 'json'){

        }
    });
}

当这一行运行时......

if ($this->response_type == 'json'){

...我指的是控制器,但不知怎的,它正在调用过滤器,所以我不能对父对象做任何事情。

有什么方法可以调用控制器类吗?

感谢。

1 个答案:

答案 0 :(得分:0)

如果使用闭包(匿名函数),则可以指定要使用的“外部”变量,否则闭包的范围是本地的。

我的猜测,这应该对你有用

class ApiController extends BaseController {

public function __construct()
{
    $ctrl = $this;
    $this->afterFilter(function() use ($ctrl)
    {
        if ($ctrl->response_type == 'json')
        {
        //do stuff
        }
    });
}

很抱歉这个混淆 - 毕竟这篇文章与Laravel 4: Reference controller object inside filter

重复