CodeIgniter Forums

Full Version: Codeigniter + namespaces
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]porquero[/eluser]
I've patched CI to make support namespaces. Only replace Codeigniter.php file with patch. And Code your controllers:

Code:
namespace controllers\blog

class user extends \CI_Controller {
  public function get_comments($id_user){
     // Code  
  }
}

$_ns = __NAMESPACE__;

Download patch:
http://dl.dropbox.com/u/315893/blog/CI+H...gniter.php

More info:
http://porquero.blogspot.com/2012/03/cod...art-1.html

El Forum

[eluser]Noobigniter[/eluser]
Thank you, works fineā€¦

El Forum

[eluser]InsiteFX[/eluser]
Now what do you think is going to happen when you upgrade CodeIgniter?

You never touch the CodeIgniter core files!

El Forum

[eluser]Noobigniter[/eluser]
Hi,

Yes, I realize it will take (or not?) Make changes in an update, but it was also the case in the files of my application (Model => CI_Model, ...).
The change only two lines codeigniter.php.
I put it on github as "personal" backup.
If you want to see the changes.
(This is mainly to test namespaces story to learn.)

El Forum

[eluser]porquero[/eluser]
[quote author="InsiteFX" date="1332392830"]Now what do you think is going to happen when you upgrade CodeIgniter?

You never touch the CodeIgniter core files!
[/quote]

CI 2 hasn't full support for php5.3, so meanwhile. And I understand that the next version will be 3.

El Forum

[eluser]porquero[/eluser]
[quote author="Noobigniter" date="1332396275"]Hi,

Yes, I realize it will take (or not?) Make changes in an update, but it was also the case in the files of my application (Model => CI_Model, ...).
The change only two lines codeigniter.php.
I put it on github as "personal" backup.
If you want to see the changes.
(This is mainly to test namespaces story to learn.)[/quote]

Thanks for test it, I accept any feedback.

I tried to make Model support NS but I could not do it.

El Forum

[eluser]Noobigniter[/eluser]
hello,
I tested with libraries and Controllers, but not Models ...
I thought it was good lol, at the same time this is new for me.
After testing, namespaces do not work in Models, but this is not too worried because we know still use them.

El Forum

[eluser]goFrendiAsgard[/eluser]
[quote author="porquero" date="1331088931"]I've patched CI to make support namespaces. Only replace Codeigniter.php file with patch. And Code your controllers:

Code:
namespace controllers\blog

class user extends \CI_Controller {
  public function get_comments($id_user){
     // Code  
  }
}

$_ns = __NAMESPACE__;

Download patch:
http://dl.dropbox.com/u/315893/blog/CI+H...gniter.php

More info:
http://porquero.blogspot.com/2012/03/cod...art-1.html[/quote]

If this is merged to official release, CodeIgniter will be better. Just add "action_index" instead of "index", and it will be (may be) as good as FuelPHP

El Forum

[eluser]porquero[/eluser]
[quote author="Noobigniter" date="1332609235"]hello,
I tested with libraries and Controllers, but not Models ...
I thought it was good lol, at the same time this is new for me.
After testing, namespaces do not work in Models, but this is not too worried because we know still use them.[/quote]

The reason that I decided to modify core files is because I'm working with hmvc extension, and class collision is more probable. For this reason I also modified hmvc extension.
Then you can work with namespaced hmvc:

Code:
namespace controllers\blog

class user extends \CI_Controller {
  public function get_comments($id_user){
     /* Code */

     //[1]
     $module = $this->load->module('namespace\modulename/classname');
     //[2]
     $module->method($x, $y);

     // OR [3]
     \Modules::run('namespace\modulename/classname/method', $x, $y);
  }
}

$_ns = __NAMESPACE__;

Wich is the advantage?

Well, with hmvc you can work with micro-applications, for example:

modules/blog
modules/wiki

And is probable that both modules (micro-applications) has a class called User. If you need use a blog/user class into a wiki/user class, is posible a name collision, but using the patch you don't need woory about this:

Code:
namespace wiki

class user extends \MX_Controller{

// Code.

$user_blog = $this->load->module('blog\user');
$user_blog->get_info();

// Code.
}

I'm going continue investigate how to make possible namespace support for modules.

I hope that it help you! Thanks.

More info:
http://porquero.blogspot.com/2012/03/cod...art-2.html