【问题标题】:using function in name space without back-slash在没有反斜杠的命名空间中使用函数
【发布时间】:2014-12-07 18:24:32
【问题描述】:

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

我将它放在名称空间控制器/管理员中;

但是正如你在下面看到的,我不能使用没有“\”的标准 Laravel 类 请参阅\查看

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

但我想将它用作没有“\”的视图,我该怎么做?把所有View都修复成\View

确实是个问题

谢谢大家。

【问题讨论】:

    标签: php laravel namespaces


    【解决方案1】:

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

    添加:

    use View;
    

    在文件的开头,例如:

    <?php
    
    namespace yournamespacehere;
    
    use View;
    

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

    如果您需要更多解释,可以查看How to use objects from other namespaces and how to import namespaces in PHP

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 2011-06-18
      • 1970-01-01
      相关资源
      最近更新 更多