laravel 不能使用 Illuminate\Routing\Controller 作为控制器,因为该名称已被使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27319560/
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
Cannot use Illuminate\Routing\Controller as Controller because the name is already in use
提问by Marcus Ruddick
I have been learning to use Laravel, watching Larcasts and using the Docs, I came across a lesson where Eloquent is being described but I'm stuck with the error:
我一直在学习使用 Laravel,观看 Larcasts 和使用文档,我遇到了一个描述 Eloquent 的课程,但我遇到了错误:
at HandleExceptions->fatalExceptionFromError(
array(
'type' => '64',
'message' => 'Cannot use Illuminate\Routing\Controller as Controller because the name is already in use'
)
)
I'm very confused and have now copied the examples provided exactly but I still get the error. I am using Laravel 5, so I don't know if there has been some undocumented change or If I am simply doing something wrong. I haven't found anything related in google searches that solve the issue so I was hoping someone here might be able to help. Here is the code that is producing the error:
我很困惑,现在已经完全复制了提供的示例,但我仍然收到错误消息。我正在使用 Laravel 5,所以我不知道是否有一些未记录的更改,或者我是否只是做错了什么。我在谷歌搜索中没有找到任何相关的东西可以解决这个问题,所以我希望这里有人可以提供帮助。这是产生错误的代码:
<?php namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use App\VarName;
class VarController extends Controller {
public function Var()
{
$Variable = VarName::get();
dd($Variable);
}
}
According to the documentation, this should work, and in the video that I watched, it did work.. what am I missing?
根据文档,这应该有效,在我观看的视频中,它确实有效..我错过了什么?
I tried deleting the Controller class, since it seems to be whats causing the already in use error, which broke everything, reinstalled and tried to just use Controller since it extends the eloquent model but now its saying:
我尝试删除 Controller 类,因为它似乎是导致已经使用错误的原因,它破坏了一切,重新安装并尝试只使用 Controller,因为它扩展了 eloquent 模型,但现在它说:
ErrorException in Pluralizer.php line 258: call_user_func()
expects parameter 1 to be a valid callback, function mb_strtolower
not found or invalid function name
Pluralizer.php 第 258 行中的 ErrorException:call_user_func()
期望参数 1 是一个有效的回调,函数mb_strtolower
未找到或函数名称无效
which is beyond my understanding of the inner workings of Laravel, I'm stuck and I don't understand the problem, according to documentation I don't see anything wrong with my code, this seems like such a simple step. all I'm trying to do is retrieve info from a database, what is going on?
这超出了我对 Laravel 内部工作原理的理解,我被卡住了,我不明白问题所在,根据文档,我没有发现我的代码有任何问题,这似乎是一个简单的步骤。我要做的就是从数据库中检索信息,这是怎么回事?
Thanks in advance for any help!
在此先感谢您的帮助!
回答by patricus
The use Illuminate\Routing\Controller;
statement is failing because there is already a Controller
class in the App\Http\Controllers
namespace.
该use Illuminate\Routing\Controller;
语句失败,因为命名空间中已经有一个Controller
类App\Http\Controllers
。
To solve the immediate issue, you can change namespace shortcut on the use statement:
要解决当前的问题,您可以更改 use 语句上的命名空间快捷方式:
use Illuminate\Routing\Controller as BaseController;
However, the solution for your specific issue is that you probably just want to remove the use Illuminate\Routing\Controller;
statement altogether.
但是,针对您的特定问题的解决方案是您可能只想use Illuminate\Routing\Controller;
完全删除该语句。
In Laravel 5, the App\Http\Controllers\Controller
class already extends the Illuminate\Routing\Controller
class. The intention is that all new controllers should then extend the App\Http\Controllers\Controller
class. For example, take a look at the default App\Http\Controllers\HomeController
or App\Http\Controllers\WelcomeController
, as both extend the App\Http\Controllers\Controller
class.
在 Laravel 5 中,App\Http\Controllers\Controller
该类已经扩展了Illuminate\Routing\Controller
该类。目的是所有新控制器都应该扩展App\Http\Controllers\Controller
该类。例如,看看默认的App\Http\Controllers\HomeController
or App\Http\Controllers\WelcomeController
,因为它们都扩展了App\Http\Controllers\Controller
类。
In summary, your two options are:
总之,您的两个选择是:
// rename the class in the use statement
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
// note the name of the class being extended
class VarController extends BaseController {
// snip
}
Or
或者
// extend the existing App\Http\Controllers\Controller class
namespace App\Http\Controllers;
class VarController extends Controller {
// snip
}