小编daz*_*per的帖子

如何从非控制器方法的方法发送响应?

我有一个Controller.php,其show($id)方法是通过路由击中.

public function show($id)
{
    // fetch a couple attributes from the request ...

    $this->checkEverythingIsOk($attributes);

    // ... return the requested resource.
    return $response;
}
Run Code Online (Sandbox Code Playgroud)

现在,在checkEverythingIsOk(),我执行一些验证和授权的东西.这些检查对于同一控制器中的多个路由是通用的,因此我想在每次需要执行相同操作时提取这些检查并调用该方法.

问题是,我无法从这个方法发送一些回复:

private function checkEverythingIsOk($attributes)
{
    if (checkSomething()) {
        return response()->json('Something went wrong'); // this does not work - it will return, but the response won't be sent.
    }

    // more checks...
    return response()->callAResponseMacro('Something else went wrong'); // does not work either.

    dd($attributes); // this works.
    abort(422); // this works …
Run Code Online (Sandbox Code Playgroud)

laravel laravel-5 laravel-5.2

10
推荐指数
4
解决办法
4292
查看次数

标签 统计

laravel ×1

laravel-5 ×1

laravel-5.2 ×1