Welcome Guest, Not a member yet? Register   Sign In
Call model-class from view
#11

[eluser]Dam1an[/eluser]
Seeing as you have the login form on every page, you need to always track what page the user is on
To get the current URI use
Code:
$this->uri->uri_string()
#12

[eluser]juan1904[/eluser]
Alright, I do get that.

And then I want to check if the user is logged in to view the right page for him like this:

login.php:
Code:
if(isset for session varibles)
{
// Welcome message and stuff!
}
else
{
// Show the login form.
}

Then I need my viewfile to know about sessions. And TheFuzzy0ne said that the View should only be aware of the Controller. So this should be classified as bad policy for the MVC-model. Am I correct?

How should I solve this problem?
#13

[eluser]Dam1an[/eluser]
There are 2 solutions
1) Check if they're logged in in the controller, if so, load the logged_in view, if not, load the login form view
2) Do the check in the view, but don't do it by calling the session directly
In some of my views, I call a helper (User::is_admin()) and if so, load the aditional admin view as well as the normal user view

The second option is more likely to get some controversy going, but its not dirty in the sense you don't access the session variables directly... Sometimes its best to do some logic like this in the view

Because yours is so simple, the first solution should be more then adequate
#14

[eluser]juan1904[/eluser]
Thank you for a good answer.

Option number one seem to be a little bit unnessecary since I need to check if the user is logged in, in every singel Controller. Number two seem more like what I need! That helper of yours is it a home made or is it already integrated in CodeIgniter?
#15

[eluser]Dam1an[/eluser]
Its one of my own
Its just a wrapper for the session class, but it hides the underlying logic from the view
#16

[eluser]juan1904[/eluser]
Hmm OK. I don't really know what you mean by that. Is there a code example of such a helper-class somewhere?
#17

[eluser]Dam1an[/eluser]
Mine is
Code:
public static function is_admin() {
    $CI =& get_instance();
    return $CI->session->userdata('is_admin');
}

By making the function static, I call it by User::is_admin()
If you want to just calll is as is_admin(), use
function is_admin() {
$CI =& get_instance();
return $CI->session->userdata('is_admin');
}
[/code]
#18

[eluser]juan1904[/eluser]
Hm alright. Have you created a helpclass for this or have you placed it in an already existing class?
What does the line "$CI =& get_instance();" mean, I've tried to google it but without result.
Don't you need to load a helper file to use this function?

I am a little confused now. Wink
#19

[eluser]Dam1an[/eluser]
I put this in my own helper file (/application/helpers/user_helper.php)
The get_instance line makes a reference to the CI super object, this is needed, as its not available in the scope of a helper, as the helper doesn't extend CI_base
And yes, I autoload that helper as its also got a check for logged_in which happens on every page
#20

[eluser]juan1904[/eluser]
Okey, I think I know how to implement it. Thank you!
Do you know any website where I can read about how CodeIgniter is built so I can learn to solve these kind of problems on my own in the future?

Just one more question, that &-sign in $CI =& get_instance(); is that a part of the function name or is it something else?




Theme © iAndrew 2016 - Forum software by © MyBB