34

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

5 Answers 5

98

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;
2
  • @lukeasgeiter, would you prefer to use /View::make() in the controller method or the use View declaration?
    – laketuna
    May 25, 2015 at 1:02
  • 4
    @musicliftsme Definitely use View. Or maybe even the view() helper function. May 25, 2015 at 7:04
25

In Laravel 5.1 the correct use code would be:

use Illuminate\Support\Facades\View;

2
  • Thanks, I have solved problem. Would you please do me a favor? I have js and css files in resource directive(resource dir that consist asset, views,land dirs). I need to load js and css files from views/index.html. Please help me. Feb 1, 2017 at 12:11
  • 1
    try this in laravel 6.2 and its worked for me, thanks alot Mar 27, 2020 at 11:00
9

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.

0
2

For me it was namespace problem. I used php artisan to create controller but it seems like php artisan used different namespace (may be I have to change something in composer.json to fix it but I am totally new in laravel)

Whoops, looks like something went wrong. FatalErrorException in PagesController.php line 11: Class 'App\Http\Controllers\Controller' not found Good that I am using phpStorm which automatically inserted proper namespace

make sure you check out namespace properly. This is how I had controller created with php artisan


    namespace App\Http\Controllers;
    use App\Http\Requests;
    use App\Http\Controllers\Controller; //php artisan inserted.
    class PagesController extends Controller
    {
        public function index(){
            return view('index');
        }
        public function about(){
            return view('pages.about');
        }
    }

and this is how phpstorm inserted after I manually wrote extends controller


    namespace App\Http\Controllers;
    use App\Http\Requests;
    use Illuminate\Routing\Controller; //I manually wrote extends Controller and selected this namespace 

    class PagesController extends Controller
    {
        public function index(){

     ...

0

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.