问题描述
我正在将我的项目从laravel 4.2更新到laravel 5.0.但是,在我遇到此错误并在过去4个小时内一直尝试解决该错误之后.
I was updating my project from laravel 4.2 to laravel 5.0. But, after I am facing this error and have been trying to solve it for the past 4 hours.
我在4.2版本上没有遇到任何类似的错误.我尝试过作曲家dump-autoload
没有任何效果.
I didn't face any error like this on the 4.2 version. I have tried composer dump-autoload
with no effect.
如更新指南中所述,我已经按原样移动了所有控制器,并将app/Providers/RouteServiceProvider.php
中的namespace
属性设置为null
.因此,我想我所有的控制器都在全局名称空间中,因此不需要在任何地方添加路径.
As stated in the guide to update, I have shifted all the controllers as it is, and made the namespace
property in app/Providers/RouteServiceProvider.php
to null
. So, I guess all my controllers are in global namespace, so don't need to add the path anywhere.
这是我的composer.json:
Here is my composer.json:
"autoload": {
"classmap": [
"app/console/commands",
"app/Http/Controllers",
"app/models",
"database/migrations",
"database/seeds",
"tests/TestCase.php"
],
页面控制器:
<?php
class PagesController extends BaseController {
protected $layout = 'layouts.loggedout';
public function getIndex() {
$categories = Category::all();
$messages = Message::groupBy('receiver_id')
->select(['receiver_id', DB::raw("COUNT('receiver_id') AS total")])
->orderBy('total', 'DESC'.....
这里是BaseController.
And, here is BaseController.
<?php
class BaseController extends Controller {
//Setup the layout used by the controller.
protected function setupLayout(){
if(!is_null($this->layout)) {
$this->layout = View::make($this->layout);
}
}
}
在routes.php中,我按如下方式调用控制器:
In routes.php, I am calling controller as follows :
Route::get('/', array('as' => 'pages.index', 'uses' => 'PagesController@getIndex'));
任何人都请帮忙.过去几个小时,我一直在挠头.
Anyone please help. I have been scratching my head over it for the past few hours.
推荐答案
路由已加载到 app/Providers/RouteServiceProvider.php 文件中.如果您在其中查看,则会看到以下代码块:
Routes are loaded in the app/Providers/RouteServiceProvider.php file. If you look in there, you’ll see this block of code:
$router->group(['namespace' => $this->namespace], function($router)
{
require app_path('Http/routes.php');
});
这会将名称空间添加到任何路由中,默认情况下为App\Http\Controllers
,因此会出现错误消息.
This prepends a namespace to any routes, which by default is App\Http\Controllers
, hence your error message.
您有两个选择:
- 在控制器顶部添加适当的名称空间.
- 加载组中外部的路由,因此不会自动添加名称空间.
- Add the proper namespace to the top of your controllers.
- Load routes outside of the group, so a namespace isn’t automatically prepended.
我会选择选项#1,因为从长远来看,它可以避免您的头痛.
I would go with option #1, because it’s going to save you headaches in the long run.
这篇关于将Laravel 4.2升级到5.0,获得[ReflectionException]类App \ Http \ Controllers \ PagesController不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!