简体   繁体   English

如何在Laravel中“导入”名称空间?

[英]How to “import” namespaces in Laravel?

I've built an API in Laravel, versioned as follows: 我在Laravel中建立了一个API,版本如下:

/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? 这行得通,但是由于我在控制器中使用名称空间,因此必须使用\\方法才能在根名称空间中找到像\\ Response这样的类?

 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; 我尝试使用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');
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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