Jump to content


Photo

Tank Auth In Code Igniter - How To Get The User's Id In Any Other Controller?


  • Please log in to reply
11 replies to this topic

#1 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 25 November 2012 - 02:44 PM

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?
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

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 Scott_S

Scott_S

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts
  • LocationMilwaukee, WI

Posted 26 November 2012 - 09:16 AM

session/cookie

#3 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

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.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

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 Scott_S

Scott_S

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts
  • LocationMilwaukee, WI

Posted 26 November 2012 - 04:56 PM

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?

#5 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 26 November 2012 - 05:06 PM

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 :/ 
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

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 Scott_S

Scott_S

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts
  • LocationMilwaukee, WI

Posted 26 November 2012 - 05:23 PM

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

#7 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 26 November 2012 - 05:29 PM

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 by Jessica, 26 November 2012 - 05:29 PM.

My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

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 Scott_S

Scott_S

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts
  • LocationMilwaukee, WI

Posted 26 November 2012 - 05:56 PM

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.

#9 Mahngiel

Mahngiel

    Advanced Member

  • Members
  • PipPipPip
  • 1,076 posts

Posted 26 November 2012 - 07:44 PM

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 by Mahngiel, 26 November 2012 - 07:45 PM.

?????????????????? This is a signature ????????????????????????

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

CodeIgniter Libraries: Image Lib | Paginator Lib | AJAX Upload

#10 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 26 November 2012 - 09:57 PM

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 by Jessica, 26 November 2012 - 10:00 PM.

My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

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 Scott_S

Scott_S

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts
  • LocationMilwaukee, WI

Posted 28 November 2012 - 04:10 AM

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.

#12 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 28 November 2012 - 06:16 PM

I switched to CakePHP. Way more what I'm used to :)
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

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!!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com