【问题标题】:how to setup routes, controller, and views the proper way in Laravel?如何在 Laravel 中正确设置路由、控制器和视图?
【发布时间】:2015-07-18 17:41:59
【问题描述】:

我刚开始玩 laravel。首先,这是我遇到的最好的框架。现在这是我的问题。我试图从路线指向-> 控制器-> 视图

//This is my Controller file

 public function index()
{
    return View::make('pages', array('name' => 'Taylor'));
}

 // This is my Routes File
Route::get('/', 'pagesController@index');

View file => pages.blade.php

这是我遇到的错误。

FatalErrorException in pagesController.php line 19:
Class 'App\Http\Controllers\View' not found

【问题讨论】:

    标签: php laravel laravel-5 namespaces


    【解决方案1】:

    从错误看来,问题在于当前命名空间中没有找到View类。

    试试这个方法:

    //use the View class from the global namespace
    return \View::make('pages', array('name' => 'Taylor'));
    

    或者在控制器脚本的开头导入View类:

    use View;
    

    【讨论】:

    • 是的!有效。谢谢你,但我的问题是,为什么它需要路径?
    • 因为文件的当前命名空间是App\Http\Controllers,如果你没有另外指定,Laravel 会在当前命名空间中搜索类的文件。我建议您检查namespaces 在 PHP 中的工作原理
    • @HimanshuBatra:如果有用,请考虑接受答案;)
    猜你喜欢
    • 1970-01-01
    • 2012-12-17
    • 2019-08-23
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多