12

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?

1 Answer 1

13

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Works fine. I'll accept your answer after 9 minutes (not allowed to accept it right now). Just another quick question, what's the difference between view and View::make
View::make() is from Laravel 4, this changed in 5 to the view() helper function as the use of facades is discouraged, although you can still use them for now. I'd expect they will become deprecated at some point if they're not already.
@n0t_a_nUmb3R- I have updated my answer with relevant links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.