Class App/Http/Controllers/View Not Found error

2019-04-03 07:37发布

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

标签: laravel-5
5条回答
Explosion°爆炸
2楼-- · 2019-04-03 07:54

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(){

     ...

查看更多
唯我独甜
3楼-- · 2019-04-03 07:59

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.

查看更多
神经病院院长
4楼-- · 2019-04-03 08:01

In Laravel 5.1 the correct use code would be:

use Illuminate\Support\Facades\View;

查看更多
狗以群分
5楼-- · 2019-04-03 08:05

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;
查看更多
再贱就再见
6楼-- · 2019-04-03 08:15

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.

查看更多
登录 后发表回答