Object Programming with CodeIgniter |
Hey everybody,
I was wondering how to deal with plain PHP object and CodeIgniter framework, so here is what I did first : I created a PHP Class in libraries folder for my object, "User" in that case, with accessors. So far everything was ok with my Class, I tested it in a separate test environement without CI. Then I tried to integrate it to my project so in my controller I included with a PHP Code: $this->load->library("User"); But then I was only able to have one instance of User at a time ! That's fine for a User I think but I will have to deal with a lot of instances of some objects and so I "tricked CI" by including in the controller ![]() PHP Code: require(....); And that way I was able to have multiple User objects but then I realized that I wanted to use my simple CRUD overriding of CI_Model into my User Class, that way I would be able to make the code for a user authentication very small in controller and easy to understand and also to maintain (yes, it's becoming a huge crap in my controllers, also I splitted a lot my operations into small functions, it's harder and harder to make modifications and most of time it breaks something else when I add/mod something). So, I thinked about it and told myself Quote:Ok, think about your classes, an Object here represents one or more entries in DB, so It must be between the Model and the Controller. That way I ended with a User_model but had the same problem with the Loader Class because only one instance was "allowed"... So I made a User Class which extends a User_Model Class which extends MY_Model Class which extends CI_Model Class............... ![]() I don't explain you what CI_Model does... MY_Model : PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); User_model PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); User PHP Code: <?php By the way, I can use $this to call CI Classes like session, db, and others as well as MY_Model functions. I also can change the table while running with the protected accessors for $table field of MY_Model. Then my login function is a lot easier to understand : PHP Code: public function login() { SO ! Everything works very well now and I love to abstract a Web Application, objects are so cool ![]() ![]() I was just wondering if any of you works with your own-baked Objects. If you are, please share the stuff ! And also, if anyone has anything to say about my approach : tell me, I wan't to here your thoughts about this way of working ! Thanks to read ![]()
Greate tell me more.. And what is the library you used in $this->layout ???? I am searching template library
(04-28-2015, 12:57 AM)Vimal Wrote: Greate tell me more.. And what is the library you used in $this->layout ???? I am searching template library Concerning the Layout it's a library which is auto-loaded and working with an asset helper (also auto-loaded) and layout views. Layout Class (in application/libraries folder) PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); Asset Helper Default Layout View (in application/views/layouts folder, you have to create the layouts one) PHP Code: <!doctype html> Then you are free to add for example a menuMaker like I did (it simply use a config array and build all links with the makeMenu() function) or whatever you think would be useful ! I took the Layout Class from the internet but can't manage to find it again so sorry for no credits...
Sounds like you came up with a pretty good solution in the end.
![]() I have become a huge fan of Composer. To handle the situations like yours, I would enable Composer in CodeIgniter's config file Code: $config['composer_autoload'] = TRUE;[/url] Then, I would make that User class, still located in application/libraries, to have a namespace of App\Libraries. Code: <?php namespace App\Libraries; Then, in your other classes all you have to do is create a new User object. Code: $user = new App\Libraries\User( $this->user_model ); Something like that anyway. I'm still revising my tasks to be easier for testing, but I believe that would all work. While I probably went overboard there, hopefully you see the main point of using Composer as an autoloader. ![]() |
Welcome Guest, Not a member yet? Register Sign In |