php - Stop laravel controller action in another class

467

Imagine I have aPostController 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() {

        // I want to send the response directly from here
        // but it doesn't work
        // I tried:
        // return response()->json(['error' => 'auth error'], 403);
        // the line above is not working, that's all I want
        // NOTE: abort(403) works here
        // NOTE: dd('xxxxx') works here
        // but response() doesn't

    }
}

I can fix this by returning the value in myPostsController:

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 mycheck() method, I don't want that logic in my controller and that's why I am doing this.

660

Answer

Solution:

You can put it in aif condition like this:

if(!$newClass->check()) {
     return false; //replace this with whatever you want
}

This way it will check if the$newClass->check() returnsfalse, and if it does it will stop the execution inside thestore method.

936

Answer

Solution:

In MyClass file

class MyNewClass
{
    public function check() {
        return false;
    // I want to send the response directly from here
    // but it doesn't work
    // I tried:
    // return response()->json(['error' => 'auth error'], 403);
    // the line above not working, that's all I want
    // NOTE: abort(403) works here
    // also dd('xxxxx') works here
    // but response() doesn't
        return response();
    }
}

then in your controller check the return

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

        $newClass = new myNewClass();

        if($newClass->check()) {

            // ... 
            // continue to store the post
            // ...     
            return $newClass->check();
        }
    }
}

I think this code should work with you

note: if it works fine, try this code for better performance.

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

        $newClass = new myNewClass();

        if($return = $newClass->check()) {

            // ... 
            // continue to store the post
            // ...     
            return $return;
        }
    }
}

People are also looking for solutions to the problem: Next row function in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.