简体   繁体   中英

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

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');
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM