Support the ongoing development of →
Last updated 1 year ago.

You could use \Response::json and no use statement is needed.

Or you could create a shared BaseController and then extend all other controllers from it ...

namespace App\Http\Controllers;

use Response;
use Illuminate\Routing\Controller;
use App\Http\Requests\FormTestValidation;

class BaseController extends Controller {

/* any other shared functions can go here */


and then for other controllers

namespace App\Http\Controllers;

class IndexController extends BaseController {

/* ... */


Hope that helps.


I am getting a "Class 'App\Http\Controllers\Response' not found" from this solution.

Any thoughts on that?


Namespaces and use statements are file-scoped. There's no way to have other files "inherit" a use.

One option would be something like this in your BaseController:

function prepareResponse($response) {
    return Response::json($response);

Then in your other controller functions:

return $this->prepareResponse($responseData);

use Response at your BaseController and then use namespace above your controller.


put \ before your Response like :

return \Response::json($response);

It will work.


As was already stated, imports are file-scoped, there is no inheritance. Every file has its own imports/aliases.


Sign in to participate in this thread!


Your banner here too?

chrislentz chrislentz Joined 19 Jun 2014


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.