0

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

}
paullb
  • 4,293
  • 6
  • 37
  • 65
  • May be `Welcome` class is already declared. – Yogesh Suthar Aug 26 '13 at 06:59
  • It's the default install, I have only changed the namespace and the use. That's it. I tried changing "welcome" to "welcomeABC" and got the same error: "ErrorException [ Compile Error ]: Cannot redeclare class Fuel\Controller\WelcomeABC" – paullb Aug 26 '13 at 07:00
  • having a namespace called controller, while at the same time using a class called Controller is a bit iffy IMO... best write `use Fuel\Core\Controller as CoreController;` – Elias Van Ootegem Aug 26 '13 at 07:03
  • Elias Van Ootegem > A reasonable precaution. I tried implementing it now but got no change in results. – paullb Aug 26 '13 at 07:06

3 Answers3

1

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)

paullb
  • 4,293
  • 6
  • 37
  • 65
0

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

Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • As I said above, I have not created multiple controllers called Welcome. This is a DEFAULT INSTALL of FuelPHP with only 2 lines changed (to add the namespace). Changing the name yields the same result so it is some issue with this file being read in twice by the framework. – paullb Aug 26 '13 at 07:07
0

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.

Gadonski
  • 3,150
  • 2
  • 25
  • 31