Welcome Guest, Not a member yet? Register   Sign In
isLogged in one Controller?
#1

[eluser]FREEZA[/eluser]
Hello.

Ich want to create a Controller called Product. There you can find two methods:

Code:
createProduct(); // This method is only available for users witch are logged in
getAllProducts();// This method is available for all
I read that i could define a isLogged() method in the __Constructor of the Controller Product but then you must be logged in for all methods!

The method getAllProducts() should be available for all!

Please Help me: )
#2

[eluser]Eric Barnes[/eluser]
Why not just move your isLogged into the method you want to require login and not in the constructor?
#3

[eluser]FREEZA[/eluser]
so then i must to write this Code in every method where logged in is required?

Code:
if($this->session->userdata('logged_in')
{
   // Method Code
}
else
{
  // hey you need to be logged in!
}

hm...it is okay so?
#4

[eluser]steelaz[/eluser]
I would write this the other way around:

Code:
if ( ! $this->session->userdata('logged_in')
{
    // Set session error message

    // Redirect
    redirect('auth/login');
}

// Method code
#5

[eluser]FREEZA[/eluser]
okay, thanks for help!

What i could do when i have a lot of methods where you must to be logged in?

then i must write this code in every method:

Code:
if ( ! $this->session->userdata('logged_in')
{
    // Set session error message

    // Redirect
    redirect('auth/login');
}

// Method code


do you know a other way?
#6

[eluser]steelaz[/eluser]
Usually I have "public" controllers (login/registration pages, website front-end, etc.) and "user/admin" controllers where user has to be logged-in to access them. Logged-in check happens in controller constructor so not to repeat it in every method. If you have a lot of methods that require logged-in user and some methods that are public, you might want to consider moving public methods to another controller.
#7

[eluser]FREEZA[/eluser]
ah okay! this is a good idea! thank you
#8

[eluser]JonoB[/eluser]
Have a look at http://philsturgeon.co.uk/news/2010/02/C...ing-it-DRY




Theme © iAndrew 2016 - Forum software by © MyBB