Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel main controller return response to page

Tags:

laravel

class Controller extends BaseController
{

public function __construct(Request $request)
{
    if ( 1 == 1 ) {
      return response()->json([
          'error' => true,
          'message' => 'foo' 
      ], 400);
    }
}

I have set up some logic in my main controller __construct method for every pages to extend and I need to return response to page

but I have test this and it is not working, anyone know where is the problem?

like image 835
Benjamin W Avatar asked Oct 12 '25 09:10

Benjamin W


1 Answers

You don't return from constructors, you return the response from controller methods.

Create an index method or similar in you main controller and then setup routes through the controllers inheriting from main. This will give you a default shared response.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!