CodeIgniter Forums

Full Version: Avoid controller name collisions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
Here's a sweet trick I use with my controllers. I know CodeIgniter doesn't use namespaces (and I understand that's not the most important point), but why don't use some of the advantages of PHP 5.3 ?

Simply change in CodeIgniter.php
Code:
$class  = $RTR->fetch_class();
by
Code:
$directory = str_replace('/','\\',$RTR->fetch_directory());
$class = 'controllers\\'.$directory.$RTR->fetch_class();

Now, you can add the namespace line on the top of your controller files, and use the same name twice (or more). This trick prevents collisions from your models folder too (using namespaces for your objects is the best way to solve that issue once and for all).

What do you think about ?

El Forum

[eluser]InsiteFX[/eluser]
You should never modify any of the CI Core files, only extend them.

Now everytime there is a new update you will have to modify that file.

InsiteFX

El Forum

[eluser]Basketcasesoftware[/eluser]
He can make a code suggestion and if it's accepted then it'll come alone with whatever update. This is in keeping actually with goals of HMVC in fact. It's a good suggestion.

El Forum

[eluser]Cristian Gilè[/eluser]
http://codeigniter.uservoice.com/forums/...?ref=title


Cristian Gilè

El Forum

[eluser]Unknown[/eluser]
I've already posted on that suggestion Smile

[quote author="InsiteFX" date="1296730243"]You should never modify any of the CI Core files, only extend them.

Now everytime there is a new update you will have to modify that file.

InsiteFX[/quote]

Yeah, I know that, but the CodeIgniter.php file can't be extended, can it ?

El Forum

[eluser]Basketcasesoftware[/eluser]
And one mustn't forget that CodeIgniter is an open source project. That means a collaborative, community based effort. That means ALL files can and SHOULD be subject to editing to improve functionality or add features. That is how these projects grow and evolve. Heck! I was just thinking of a feature I'd like to add to database functionality to better support multiple data functionality over several servers and accounts. It might itself require changes to the appropriate core files to be realized. And if you read the documents it even talks about what is needed to override or replace those core files. This is how the CodeIgniter project works.

El Forum

[eluser]ntheorist[/eluser]
you could create a MY_Router.php library and override set_class and get_class to say, change the class name to whatever pattern you like. Prepend it with 'Controller_' or something, like kohana does.

El Forum

[eluser]Basketcasesoftware[/eluser]
I know I've seen that name "kohana" somewhere lately. What project is he doing? :red:

El Forum

[eluser]ntheorist[/eluser]
http://kohanaframework.org/

El Forum

[eluser]Basketcasesoftware[/eluser]
Oh, that. Sometime I'll actually look at that. Thanks for the link.