Jorre
Jorre

Reputation: 17581

How to "import" namespaces in Laravel?

I've built an API in Laravel, versioned as follows:

/controllers/api/v1/CommentController.php
/controllers/api/v2/CommentController.php

In my routes I call the correct controllers like so:

Route::resource('notification', 'api\v2\CommentController');

This works, but since I'm using namespaces in my controllers, I have to use the \ approach in order to find classes like \Response in the root namespace?

 namespace api\v1;
 class NotificationController extends \BaseController {
     public function index()()
     {
        return \Response::json({});
     }
 }

Is there a way to avoid that backslash notation and using the right namespace? I tried using use \App; but without result

Upvotes: 0

Views: 814

Answers (1)

Roy Philips
Roy Philips

Reputation: 342

U need to add "use Response;" to your code.

<?php namespace api\v1;
use Response;
class CommentController extends \BaseController {
public function index()
{
    return Response::json('hello');
}
}

Upvotes: 1

Related Questions