Tank Auth In Code Igniter - How To Get The User's Id In Any Other Controller?
#1
Posted 25 November 2012 - 02:44 PM
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#2
Posted 26 November 2012 - 09:16 AM
#3
Posted 26 November 2012 - 03:37 PM
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.
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#4
Posted 26 November 2012 - 04:56 PM
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?
#5
Posted 26 November 2012 - 05:06 PM
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 :/
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#6
Posted 26 November 2012 - 05:23 PM
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
#7
Posted 26 November 2012 - 05:29 PM
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 by Jessica, 26 November 2012 - 05:29 PM.
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#8
Posted 26 November 2012 - 05:56 PM
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.
#9
Posted 26 November 2012 - 07:44 PM
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.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.
// 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 by Mahngiel, 26 November 2012 - 07:45 PM.
CodeIgniter Libraries: Image Lib | Paginator Lib | AJAX UploadWhat do you mean "so please write the code for me . . . "? We aren't here in gleeful anticipation just waiting to write code for you, you know.
#10
Posted 26 November 2012 - 09:57 PM
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 by Jessica, 26 November 2012 - 10:00 PM.
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#11
Posted 28 November 2012 - 04:10 AM
#12
Posted 28 November 2012 - 06:16 PM
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users












