jeudi 3 mai 2018

Stop laravel controller action in another class

Imagine I have a PostController as follows:

class PostsController extends \App\Http\Controllers\Controller
{
    public function store(Request $request) {

        $newClass = new myNewClass();
        $newClass->check();

        // ... 
        // continue to store the post
        // ...     

    }
}

Now I want to $newClass->check() stops the action if something I'm checking there happens. this is just a sample code and the behavior of the mentioned methods are more than just that.

in my check method I have:

class MyNewClass
{
    public function check() {

        return false;
        // I want this to stop the store in PostsController
        // but it doesn't
        // I have also tried to send a response using Response class, but not working
        // NOTE: abort(403) works here
        // also dd('xxxxx') works here
        // but response() doesn't

    }
}

I can fix this by returning the value in my PostsController:

return $newClass->check();

But this always stops the store method and returns.

How can I achieve this?


P.S: I want to send a JSON response in my check() method, I don't want that logic in my controller and that's why I am doing this.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire