Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class App/Http/Controllers/View Not Found error

Tags:

laravel-5

I am new to laravel 5 and currently stumped by this error:

FatalErrorException in TicketController.php line 18: Class 'App\Http\Controllers\View' not found

Weird thing is the view does in fact exist, i checked to see if the route was indeed routing to the right controller and it was, the error pops up when i try to do this:

return View::make('tickets.bus.index');

It's either i am making some mistake somewhere or if the implementation is different from laravel 4

like image 372
user3718908x100 Avatar asked Mar 08 '15 20:03

user3718908x100


3 Answers

The problem is not the actual view but the class View. You see when you just reference a class like View::make('tickets.bus.index') PHP searches for the class in your current namespace.

In this case that's App\Http\Controllers. However the View class obviously doesn't exists in your namespace for controllers but rather in the Laravel framework namespace. It has also an alias that's in the global namespace.

You can either reference the alias in the root namespace by prepending a backslash:

return \View::make('tickets.bus.index');

Or add an import statement at the top:

use View;
like image 132
lukasgeiter Avatar answered Oct 19 '22 05:10

lukasgeiter


In Laravel 5.1 the correct use code would be:

use Illuminate\Support\Facades\View;

like image 24
the JinX Avatar answered Oct 19 '22 06:10

the JinX


There exists a helper-function, view(), which is in the global namespace, and may be used to simplify the syntax:

return view('tickets.bus.index');

With this method, it is unnecessary to include use View; or include the root namespace, e.g., \View.

The concepts that lukasgeiter explained are essential to understanding Laravel, even if you elect to use the helper-function.

like image 9
Ben Johnson Avatar answered Oct 19 '22 05:10

Ben Johnson