Jessica Posted November 25, 2012 Share Posted November 25, 2012 I'm using Tank Auth for my user authentication for a CI project. The login seems to work fine - how do I access the logged in user's info in another controller? Quote Link to comment https://forums.phpfreaks.com/topic/271154-tank-auth-in-code-igniter-how-to-get-the-users-id-in-any-other-controller/ Share on other sites More sharing options...
Scott_S Posted November 26, 2012 Share Posted November 26, 2012 session/cookie Quote Link to comment https://forums.phpfreaks.com/topic/271154-tank-auth-in-code-igniter-how-to-get-the-users-id-in-any-other-controller/#findComment-1395175 Share on other sites More sharing options...
Jessica Posted November 26, 2012 Author Share Posted November 26, 2012 Â I know how Sessions and Cookies work, thanks. Â The solution was to create a Controller that extends CI_Controller, and load up the tank_auth in that controller. Then make my other controllers extend that new controller instead of CI_Controller. Quote Link to comment https://forums.phpfreaks.com/topic/271154-tank-auth-in-code-igniter-how-to-get-the-users-id-in-any-other-controller/#findComment-1395301 Share on other sites More sharing options...
Scott_S Posted November 26, 2012 Share Posted November 26, 2012 Ah, now I see what you are asking. Â I always extend CI_Controller and CI_Model with my own new base classes and everything else extends off those. Â Does Tank give you acl support or something? Why did defer auth to this lib? Quote Link to comment https://forums.phpfreaks.com/topic/271154-tank-auth-in-code-igniter-how-to-get-the-users-id-in-any-other-controller/#findComment-1395340 Share on other sites More sharing options...
Jessica Posted November 26, 2012 Author Share Posted November 26, 2012 This is my first time trying to do a project with CI - and of course I started the day their site was down Fortunately their user guides were hosted elsewhere for the day.  I chose that lib because when I googled "User Authentication for Code Igniter" it got the most hits  I'm still learning how exactly CI works. I am not 100% sure this is actually saving me time with my project :/ Quote Link to comment https://forums.phpfreaks.com/topic/271154-tank-auth-in-code-igniter-how-to-get-the-users-id-in-any-other-controller/#findComment-1395344 Share on other sites More sharing options...
Scott_S Posted November 26, 2012 Share Posted November 26, 2012 I see. FYI docs come with your download/clone, unless you blew them away. The CI docs are very well organized and I find them absolutely a delight to work with. Well if this is your first rodeo with CI here are some tips: Â 1) your My_Controller and MY_Model classes are absolutely priceless. I use the same in every project I create. 2) file structure is rather important, for organization and security. I wouldn't use the suggested structure. Here is mine: Â / /application /system /public_html (doc_root) Â This keeps all MVC's outside of doc_root. The only thing sitting in public_html is essentially a single index.php file, css, img and js dirs. Â 3. htaccess is your friend! 4. turn off all debugging and error reporting for php and db stuff when going live Quote Link to comment https://forums.phpfreaks.com/topic/271154-tank-auth-in-code-igniter-how-to-get-the-users-id-in-any-other-controller/#findComment-1395349 Share on other sites More sharing options...
Jessica Posted November 26, 2012 Author Share Posted November 26, 2012 (edited) I'm having trouble figuring out how to get models or controllers to interact with one another. In the frameworks I'm used to each model represents a table. It seems like CI suggests the same.  So here are some tables I have: users quests users_quests  users_quests contains a PK, a FK to users and an FK to quests (and some DATETIMEs).  I'm in my Quests Controller. index.php/quests/view/5 (5 = pk on users_quests)  What is the smart way to pull all of the users_quests data and the related quests_data.  I know how to do it in pure SQL, and I know how to do it in Yii - I don't get how to do it in CI, automatically anyway. I can write a chained DB statement: pseudo code as $this->db->select('fields')->from('users_quests')->join('quests', 'columns')->where('col=5');  That seems like a ton of stupid code for a simple BELONGS TO. Edited November 26, 2012 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/271154-tank-auth-in-code-igniter-how-to-get-the-users-id-in-any-other-controller/#findComment-1395352 Share on other sites More sharing options...
Scott_S Posted November 26, 2012 Share Posted November 26, 2012 I agree model/table relationship. However I occasionally have models that don't have tables. Â So how I would do this or actually how I do do this would be like this: Â quest controller: $this->load->model('quest'); Â $options = array('quest_id' =>$this->uri->segment(3)->, 'limit' => '1'); $quest = $this->quest->retrieve($options); Â quest model: this is where all the heavy lifting is done, the idea here is FAT models and SKINNY controllers Lots of code in the model, this is where having a solid MY_Model comes in to play. Â I have a more detailed answer but I am running late and have to go, I will post some more later. Quote Link to comment https://forums.phpfreaks.com/topic/271154-tank-auth-in-code-igniter-how-to-get-the-users-id-in-any-other-controller/#findComment-1395357 Share on other sites More sharing options...
Mahngiel Posted November 27, 2012 Share Posted November 27, 2012 (edited) I'm having trouble figuring out how to get models or controllers to interact with one another. In the frameworks I'm used to each model represents a table. It seems like CI suggests the same. I imagine this is because you're used to ORM. This isn't so much the case with CI, and I bet $20 you'll really like their db driver. // Random Controller $this->load->model('poll_model', 'Polls'); $Poll = $this->Polls->get_poll(array('id'=>3)); // Poll_model function get_poll($data = array()) { // Check for valid data if(empty($data) OR !is_associative($data)) { // Invalid data, return FALSE return FALSE; } // Retrieve the query from the database $query = $this->db->where($data)->get('polls', 1); // Check if query row exists if($query->row()) { // Query row exists, return query row return $query->row(); } else { // Query row doesn't exist, return FALSE return FALSE; } }  So here are some tables I have: users quests users_quests  users_quests contains a PK, a FK to users and an FK to quests (and some DATETIMEs).  I'm in my Quests Controller. index.php/quests/view/5 (5 = pk on users_quests)  What is the smart way to pull all of the users_quests data and the related quests_data.  I know how to do it in pure SQL, and I know how to do it in Yii - I don't get how to do it in CI, automatically anyway. I can write a chained DB statement: pseudo code as $this->db->select('fields')->from('users_quests')->join('quests', 'columns')->where('col=5');  That seems like a ton of stupid code for a simple BELONGS TO.  Joins are always cumbersome, and no different with CI. I would write this: // Controller $pk = $this->uri_segment(3); $this->load->model('quest_model', 'Quests'); $Data = $this->Quests->name($pk); // Model function name($id) { $this->db->select('fields')->from('users_quests')->join('quests', 'quests.user_id = users_quests.user_id')->where('users.user_id = ' . $id)->get(); } Edited November 27, 2012 by Mahngiel Quote Link to comment https://forums.phpfreaks.com/topic/271154-tank-auth-in-code-igniter-how-to-get-the-users-id-in-any-other-controller/#findComment-1395372 Share on other sites More sharing options...
Jessica Posted November 27, 2012 Author Share Posted November 27, 2012 (edited) That's what I have now. Â In the framework I've used before, the model will have an array of relations, and you say this model is a many-to-many or a belongs to. Then you just access it as $model->relatedmodel. And it's automagic. Â Why aren't I using that framework? Who knows. I like to make things harder on myself I guess. Â Edit: I'm not trying to hate on CI, I just am having trouble understanding what effort this is saving me. Why is this better than writing the SQL myself? I get using a framework that actually generates the joins and everything, but... Â SELECT fields FROM table LEFT JOIN table_2 on table_2.pk = table.fk WHERE pk = 1 or $this->db->select('fields')->from('users_quests')->join('quests', 'quests.user_id = users_quests.user_id')->where('users.user_id = ' . $id)->get(); or: $model->relation['table2']; $model->table2; Â I don't see why CI's way is helping me write code faster, and automate tasks. That's the point of a framework? Edited November 27, 2012 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/271154-tank-auth-in-code-igniter-how-to-get-the-users-id-in-any-other-controller/#findComment-1395393 Share on other sites More sharing options...
Scott_S Posted November 28, 2012 Share Posted November 28, 2012 There use to be a ORM for CI called Doctrine, you might want to check into that. ORM conversations, whether to use one or not, can get crazy. Like discussing what is the best editor/IDE is between coders. I will never understand those heated editor flame wars, when everybody knows vim is the best ... i guess they are arguing over 2nd place. Quote Link to comment https://forums.phpfreaks.com/topic/271154-tank-auth-in-code-igniter-how-to-get-the-users-id-in-any-other-controller/#findComment-1395847 Share on other sites More sharing options...
Jessica Posted November 28, 2012 Author Share Posted November 28, 2012 I switched to CakePHP. Way more what I'm used to Quote Link to comment https://forums.phpfreaks.com/topic/271154-tank-auth-in-code-igniter-how-to-get-the-users-id-in-any-other-controller/#findComment-1396059 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.