Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Returning a view from a controller

Tags:

php

laravel

xampp

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?

like image 464
n0t_a_nUmb3R Avatar asked Jul 24 '15 14:07

n0t_a_nUmb3R


People also ask

How do I return a response in Laravel?

Laravel provides several different ways to return response. Response can be sent either from route or from controller. The basic response that can be sent is simple string as shown in the below sample code. This string will be automatically converted to appropriate HTTP response.

How can we call controller method from view in Laravel?

$varbl = App::make("ControllerName")->FunctionName($params); to call a controller function from a my balde template(view page).

How do I load a view in Laravel?

Loading a view in the controller is easy. You just need to add `view('viewname')` method while returning from the controller method. `view()` is a global helper in Laravel. In this method, you just need to pass the name of the view.

How do I return a view from a controller in Laravel?

Views may also be returned using the View facade: use Illuminate\Support\Facades\View; return View::make('greeting', ['name' => 'James']); As you can see, the first argument passed to the view helper corresponds to the name of the view file in the resources/views directory.


1 Answers

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.

like image 85
pinkal vansia Avatar answered Oct 07 '22 10:10

pinkal vansia