无法使用Illuminate \ Routing \ Controller作为控制器,因为该名称已被使用 [英] Cannot use Illuminate\Routing\Controller as Controller because the name is already in use

查看:201
本文介绍了无法使用Illuminate \ Routing \ Controller作为控制器,因为该名称已被使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习使用Laravel,观看Larcasts和使用文档,我遇到了一个描述Eloquent的课程,但我仍然犯错误:

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:

at HandleExceptions->fatalExceptionFromError(
  array(
    'type' => '64',
    'message' => 'Cannot use Illuminate\Routing\Controller as Controller because the name is already in use'
  )
)

我很困惑,现在已经完全复制了提供的示例,但是仍然出现错误.我正在使用Laravel 5,所以我不知道是否存在一些未记录的更改,或者我只是做错了什么.我没有在Google搜索中找到任何可以解决此问题的相关信息,因此我希望这里的人能够为您提供帮助.这是产生错误的代码:

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:

<?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?

我试图删除Controller类,因为这似乎是导致已在使用中的错误的原因,该错误破坏了一切,重新安装并尝试仅使用Controller,因为它扩展了雄辩的模型,但现在却是这样:

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:

ErrorException:call_user_func()期望参数1是有效的回调,函数mb_strtolower未找到或函数名称无效

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

这超出了我对Laravel内部工作的理解,我被卡住了,我不理解这个问题,根据文档,我的代码没有发现任何问题,这看起来像是一个简单的步骤.我要做的就是从数据库中检索信息,怎么回事?

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?

在此先感谢您的帮助!

推荐答案

use Illuminate\Routing\Controller;语句失败,因为App\Http\Controllers名称空间中已经存在Controller类.

The use Illuminate\Routing\Controller; statement is failing because there is already a Controller class in the App\Http\Controllers namespace.

要解决当前问题,可以在use语句上更改名称空间快捷方式:

To solve the immediate issue, you can change namespace shortcut on the use statement:

use Illuminate\Routing\Controller as BaseController;

但是,针对您的特定问题的解决方案是您可能只想完全删除use Illuminate\Routing\Controller;语句.

However, the solution for your specific issue is that you probably just want to remove the use Illuminate\Routing\Controller; statement altogether.

在Laravel 5中,App\Http\Controllers\Controller类已经扩展了Illuminate\Routing\Controller类.目的是所有新控制器都应扩展App\Http\Controllers\Controller类.例如,看看默认的App\Http\Controllers\HomeControllerApp\Http\Controllers\WelcomeController,因为它们都扩展了App\Http\Controllers\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.

总而言之,您有两个选择:

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
}

// extend the existing App\Http\Controllers\Controller class
namespace App\Http\Controllers;

class VarController extends Controller {
    // snip
}

这篇关于无法使用Illuminate \ Routing \ Controller作为控制器,因为该名称已被使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆