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?
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.
$varbl = App::make("ControllerName")->FunctionName($params); to call a controller function from a my balde template(view page).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With