Working namespaces in a default FuelPHP installation I add the following to the welcome controller (otherwise unedited) and I start getting the error:

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. (Not the one that is displayed by default with FuelPHP but a black/grey one)

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

In one file

class Welcome extends Controller
{
   ...
}

In another file

class Welcome extends Controller
{
  ...
} 

Possible duplicate of Codeigniter Cannot redeclare class Hierarchy. See this PHP Fatal error: Cannot redeclare class

In your controllers, you dont need set the namespace. The app know the default namespace.

if you remove the namespace Fuel\Controller; this error will disappear.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top