简体   繁体   中英

Laravel 4 Nested Controller Error

I am accessing my "HomeController" with this route:

Route::get('home', 'Controllers\Main\HomeController@getHome');

and keep getting this error:

"Symfony \\ Component \\ Debug \\ Exception \\ FatalErrorException Class 'Controllers\\Main\\View' not found"

I have tried two ways to create the controller which are:

Method 1:

namespace Controllers\Main;

use BaseController;

class HomeController extends BaseController {   
    public function getHome()
    {   
        return View::make('main.home');
    }
}

Method 2:

namespace Controllers\Main;

use Illuminate\Routing\Controllers\Controller;

class HomeController extends Controller{
    public function getHome()
    {   
        return View::make('main.home');
    }
}

I have used "dump-autoload" and these seem to be using the controller in both cases otherwise an exception would have been thrown. The error pops up on both methods so I'm not quite what I'm missing.

Your issue is that View sit in the "global" namespace while you're on Controllers\\Main namespace, as you would use BaseController; , also add use View; .

Fixed this problem by changing my route to:

Route::get('home', 'HomeController@getHome');

and my Controller to:

class HomeController extends BaseController {
    public function getHome()
    {   
        return View::make('main.home');
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM