在名称空间中使用函数而不使用反斜杠

fob*_*bus 3 php namespaces laravel

我正在使用Laravel框架,我在controllers/admin/PlacesController.php下创建了一个类

我把它放在名字空间控制器/管理员;

但正如你在下面看到的,我不能使用没有"\"的标准Laravel课程.请参阅\ View

class PlacesController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $places=\Place::all();

        return \View::make('admin.places.index',compact('places'));
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return \View::make('admin.places.createOrEdit');
    }
}
Run Code Online (Sandbox Code Playgroud)

但我想用它作为View而不用"\"我怎么能这样做?将所有视图修复为\ View确实是一个问题

谢谢大家.

Mar*_*łek 5

您应该导入View类,因为它位于其他命名空间(根命名空间)中.

加:

use View;
Run Code Online (Sandbox Code Playgroud)

在文件的开头,例如:

<?php

namespace yournamespacehere;

use View;
Run Code Online (Sandbox Code Playgroud)

现在您可以在控制器中使用return View而不是return \View

如果您需要更多解释,可以查看如何使用其他命名空间中的对象以及如何在PHP中导入命名空间