Support the ongoing development of Laravel.io →
Architecture
Last updated 2 years ago.
0

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.

0

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

Any thoughts on that?

0

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);
0

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

0

put \ before your Response like :

return \Response::json($response);

It will work.

0

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

0

Sign in to participate in this thread!

Eventy

Your banner here too?

chrislentz chrislentz Joined 19 Jun 2014

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.