开发者

Codeigniter: Best practice for View accessing session

From what I've read, the View should be as simple as possible.

Is it good practice to access session variables开发者_Python百科 in the view?

ie.

// in the view
<?php if ($this->session->userdata('is_logged_in') : ?>
  // stuff

<?php endif; ?>


The straight answer to your actual question is simply: Yes, it is fine to access session variables inside the view. Because session or regular, they are exactly that, a variable. A place to store information.

I do this quite often with using the $this->session->flashdata for showing messages in a defined area of the view inside the header.

The reason I say this is because the others seem to skip over your actual question to get at 'why' you asked the question, "where is the best place to check for auth?" for which Cadmus's answer is right on the head of how I handle this as well, but again, don't think you shouldn't access session "data" from the view, but checking for authentication needs to happen at the Controller level for sure.


If you don't want to put these kind of "logic" into the view (a good thing IMO), you need to but in the controller. This way, the view itself will get cleaner too:

<?php if($logged_in): ?>
do stuff
<?php else: ?>
do different
<?php endif; ?>

with $logged_in coming from the view that does all the session work. You could either write your own controller, that extends from the CI controller so that the classes extend you controller or abstract it to a seperate Session class that has some static methods. I think that extending the CI controller with your own logic seems to be the cleanest way if you do lots of session handling.


if you use this variables so much you can use a helper. And you can acces to it like:

<?php if (is_logged_in()) : ?>
     <!--your html code -->
<?php else ?>
     <!--more html code -->
<?php endif;?>

then in your helper, that is called access_helper, for example, you have:

<?php
      function is_logged_in() { 
        return $this->session->user_data('is_logged_in');
      }
?>


I am Not Sure about the best practice but I like to give my way of handling the session and views. I put the session data to check the user is logged in or not to the constructor of my controller.

then I automatically get the session validation that the page which I load from that controller is getting automatic get session cover.

 public function __construct() {
    parent::__construct();
    if (!$this->session->userdata('user_data')) {
        return redirect('login');
    } else {
               redirect('dashboard');
           }

    $this->load->model('customer_model');
}

and the for success or failed message to the view I use flash data.

private function _falshAndRedirect($successful, $successMessage, $failureMessage) {
    if ($successful) {
        $this->session->set_flashdata('feedback', $successMessage);
        $this->session->set_flashdata('feedback_class', 'alert-success');
    } else {
        $this->session->set_flashdata('feedback', $failureMessage);
        $this->session->set_flashdata('feedback_class', 'alert-danger');
    }
    return redirect('customer/view_customer');
}

here I use the private function to get my message to the view.

then you create the functions and that functions get automatic the "cover of the session".

Hope this will help.


It is impossible to access a session variable from a helper. The simplest is to access the session variable from a view.

<?php if ($this->session->user_data('is_loggen_in'): ?>
    <!-- HTML stuff -->
<?php endif; ?>

In my opinion, I do not think that affects the philosophy of MVC pattern because the session is global information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜