简体   繁体   English

FuelPHP中的“无法重新声明类”问题

[英]“Cannot redeclare class” issue in FuelPHP

Working namespaces in a default FuelPHP installation I add the following to the welcome controller (otherwise unedited) and I start getting the error: 在默认的FuelPHP安装中工作的名称空间将以下内容添加到welcome控制器(否则未编辑)中,并且开始出现错误:

ErrorException [ Compile Error ]: Cannot redeclare class Fuel\Controller\Welcome"

The code I is: 我的代码是:

<?php

namespace Fuel\Controller;

use Fuel\Core\Controller;


class Welcome extends Controller
{
...
}

This is probably a beginner's question but I just can't figure out why the collision is occurring and I have tried everything I can think of. 这可能是一个初学者的问题,但是我只是想不出为什么会发生碰撞,因此我已经尽力了。

EDIT: I even tried putting the following code in front of the class and the error disappeared but a very generic looking 404 page was displayed. 编辑:我什至尝试将以下代码放在类的前面,错误消失了,但显示了一个非常普通的404页面。 (Not the one that is displayed by default with FuelPHP but a black/grey one) (不是默认情况下在FuelPHP中显示的那个,而是黑色/灰色的那个)

if (class_exists("Controller\Welcome",false)) {
//    echo "here. (" . __FILE__ . ":" . __LINE__ . ")\n";

} else {
    //Class definition...

}

The answer turned out to be that you have to change the controller prefix in the config file to the following: 答案是必须将配置文件中的控制器前缀更改为以下内容:

  'controller_prefix' => 'Controller\\',

Which is actually written in the documentation. 实际上写在文档中。 (silly me) (对不起,我)

If your application has multiple classes with the same name Welcome , then it will give error 如果您的应用程序有multiple classes具有相同名称的multiple classes Welcome ,那么它将给出error

In one file one file

class Welcome extends Controller
{
   ...
}

In another file another file

class Welcome extends Controller
{
  ...
} 

Possible duplicate of Codeigniter Cannot redeclare class Hierarchy . Codeigniter的可能重复项无法重新声明类层次结构 See this PHP Fatal error: Cannot redeclare class 看到此PHP致命错误:无法重新声明类

In your controllers, you dont need set the namespace. 在您的控制器中,您不需要设置名称空间。 The app know the default namespace. 该应用知道默认名称空间。

if you remove the namespace Fuel\\Controller; 如果删除namespace Fuel\\Controller; this error will disappear. 该错误将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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