Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Codeigniter + namespaces

March 06, 2012 9:55pm

Subscribe [4]
  • #1 / Mar 06, 2012 9:55pm

    porquero's avatar

    porquero

    66 posts

    I’ve patched CI to make support namespaces. Only replace Codeigniter.php file with patch. And Code your controllers:

    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+HMVC+NS/CodeIgniter.php

    More info:
    http://porquero.blogspot.com/2012/03/codeigniter-hmvc-namespaces-part-1.html

  • #2 / Mar 21, 2012 11:32pm

    Noobigniter's avatar

    Noobigniter

    71 posts

    Thank you, works fineā€¦

  • #3 / Mar 22, 2012 1:07am

    InsiteFX's avatar

    InsiteFX

    6819 posts

    Now what do you think is going to happen when you upgrade CodeIgniter?

    You never touch the CodeIgniter core files!

  • #4 / Mar 22, 2012 2:04am

    Noobigniter's avatar

    Noobigniter

    71 posts

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

  • #5 / Mar 22, 2012 3:05pm

    porquero's avatar

    porquero

    66 posts

    Now what do you think is going to happen when you upgrade CodeIgniter?

    You never touch the CodeIgniter core files!

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

  • #6 / Mar 22, 2012 3:11pm

    porquero's avatar

    porquero

    66 posts

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

    Thanks for test it, I accept any feedback.

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

  • #7 / Mar 24, 2012 1:13pm

    Noobigniter's avatar

    Noobigniter

    71 posts

    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.

  • #8 / Mar 28, 2012 2:21am

    goFrendiAsgard's avatar

    goFrendiAsgard

    125 posts

    I’ve patched CI to make support namespaces. Only replace Codeigniter.php file with patch. And Code your controllers:

    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+HMVC+NS/CodeIgniter.php

    More info:
    http://porquero.blogspot.com/2012/03/codeigniter-hmvc-namespaces-part-1.html

    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

  • #9 / Mar 28, 2012 9:38am

    porquero's avatar

    porquero

    66 posts

    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.

    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:

    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:

    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/codeigniter-hmvc-namespaces-part-2.html

ExpressionEngine News

#eecms, #events, #releases