Welcome Guest, Not a member yet? Register   Sign In
Avoid controller name collisions
#1

[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 ?
#2

[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
#3

[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.
#4

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


Cristian Gilè
#5

[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 ?
#6

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

[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.
#8

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB