n0t_a_nUmb3R
n0t_a_nUmb3R

Reputation: 119

Laravel: Returning a view from a controller

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:

<?php

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

Created my own MyController.php file under app\Http\Controllers and added the following code to the controller:

<?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?

Upvotes: 11

Views: 33768

Answers (1)

pinkal vansia
pinkal vansia

Reputation: 10300

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

View is a Facade, a wrapper class, and view() is a helper function that retrives a view instance.

Upvotes: 13

Related Questions