Laravel 中不存在类控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29162815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class Controller does not exist in Laravel
提问by Dave
I'm new to Laravel and I'm just following some tutorial blogs but I'm stuck at early stage.
我是 Laravel 的新手,我只是在关注一些教程博客,但我仍处于早期阶段。
Here is my Route
这是我的路线
Route::resource('ip', 'IpController');
Route::get("index","IpController@view");
and here is my controller
这是我的控制器
<?php
class IpController extends BaseController {
public function index()
{
return View::make('hello');
}
}
here is how I access the page
这是我访问页面的方式
<a href="{{ URL() }}/ip/index">IP</a>
I do what the tutorials says but I'm confused why I've got this error when accessing the page.
我按照教程说的去做,但我很困惑为什么在访问页面时出现此错误。
Class IpController does not exist
类 IpController 不存在
May i know what i've done wrong and how can i fix it? TIA!
我可以知道我做错了什么,我该如何解决?蒂亚!
回答by Erin Geyer
I came across this issue when using Lumen, not Laravel. So I thought answering here would be helpful to others who stumble onto this page as well. I apologize in advance if that is not in keeping with the SO rules.
我在使用 Lumen 而不是 Laravel 时遇到了这个问题。所以我认为在这里回答对其他偶然发现此页面的人也会有所帮助。如果这不符合 SO 规则,我提前道歉。
Here's the error I got in Lumen:
这是我在 Lumen 中遇到的错误:
lumen.ERROR: exception 'ReflectionException' with message 'Class Controller does not exist'
The solution is: when defining the route, use the full path to the controller:
解决方法是:定义路由时,使用控制器的完整路径:
$app->get('/someRoute/', 'App\Http\Controllers\Controller@index');
回答by Hammad Ahmed
Run the following in CLI:
在 CLI 中运行以下命令:
composer dump autoload
composer dump autoload
and remove the second route because it isn't necessary because it's already declared in your Resource route. and change
并删除第二条路线,因为它没有必要,因为它已经在您的 Resource 路线中声明。和改变
class IpController extends BaseController
into
class IpController extends BaseController
进入
class IpController extends eController
if you are using Laravel 5.0.
class IpController extends eController
如果您使用的是 Laravel 5.0。
回答by Anna Oleksiuk
Add a namespace.
添加命名空间。
<?php namespace App\Http\Controllers;
class IpController extends BaseController {
public function index()
{
return View::make('hello');
}
}
回答by Nati Mask
Route::resource('ip', 'IpController');
will automatically create the
将自动创建
Route::get("ip","IpController@index");
for you, along with many more helpful routes.
为您,以及更多有用的路线。
Then you can make the link to it like that:
然后你可以像这样链接到它:
<a href="{{ URL('ip') }}">IP</a>
(Because the auto-generated route made be Route::resource
uses just the www.mysite.com/ip
for "index page" for the "ip"s.
(因为自动生成的路由只Route::resource
使用www.mysite.com/ip
了“ip”的“索引页面”。
Also, in laravel 5, make sure that your controller lies in: yourapp/app/Http/Controllers/
directory.
此外,在 laravel 5 中,请确保您的控制器位于:yourapp/app/Http/Controllers/
目录中。