Laravel:从控制器返回视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31613035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:02:00  来源:igfitidea点击:

Laravel: Returning a view from a controller

phplaravelxampp

提问by n0t_a_nUmb3R

I'm trying to learn how to use Laravel 5, but I've bumped into a problem. I've created the following code so far:

under app/HTTP/routes.php:

我正在尝试学习如何使用 Laravel 5,但遇到了一个问题。到目前为止,我已经创建了以下代码:

app/HTTP/routes.php

<?php

Route::get('/', 'MyController@home');

Created my own MyController.phpfile under app\Http\Controllersand added the following code to the controller:

在下面创建了我自己的MyController.php文件app\Http\Controllers并将以下代码添加到控制器中:

<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;

class MyController extends BaseController
{
    public function home()
    {
        $name = "John Doe";
        return View::make("index")->with("name", $name);
    }
}


When I run the app I get the error:


当我运行应用程序时,出现错误:

FatalErrorException in MyController.php line 12:
Class 'App\Http\Controllers\View' not found


What am I doing wrong?


我究竟做错了什么?

回答by pinkal vansia

Change

改变

return View::make("index")->with("name", $name);

To

return \View::make("index")->with("name", $name);

or Even better

甚至更好

return view("index",compact('name'));

UPDATE

更新

Viewis a Facade, a wrapper class, and view()is a helper function that retrives a viewinstance.

View是 a Facade,一个包装类,并且view()是一个检索view实例的辅助函数。