Question

To the point, I'm trying to tighten up my code and stop repeating myself. It's a constant struggle, haha

In both my views and my controllers I am checking to see if the user is signed in. That is fine, but I would like to only have to check to see if the user is in the view and then display the right content.

Originally, in my controller, I'm seeing if the user is there and then if it is I load the same views with different varables. It seems a bit repetative.

public function recentStories(){
//This pulls back all the recent stories
    if (empty($this->user)) {
        $this->load->view('header_view', array(
        'title' => 'Recent Stories',
        ));
        $this->load->view('storyList_view', array(
        'query' => $this->data_model->pullAllStories(),
    )); 
        $this->load->view('footer_view');
    }else{
        $this->load->view('header_view',array(
            'user' => $this->users_model->getUser($this->user->user_id),
            'title' => 'Recent Stories',
        ));
        $this->load->view('storyList_view', array(
            'query' => $this->data_model->pullAllStories(),
            'admin' => $this->admin_model->pulladminRecent(),
            'user' => $this->users_model->getUser($this->user->user_id),
        )); 
        $this->load->view('footer_view');
    }
}

Ultimitly, I would like to reduce that chunk down to something like this.

public function recentStories(){
//This pulls back all the recent stories
    $this->load->view('header_view',array(
        'title' => 'Recent Stories',
    ));
        $this->load->view('storyList_view', array(
            'query' => $this->data_model->pullAllStories(),
            'admin' => $this->admin_model->pulladminRecent(),
            'user' => $this->users_model->getUser($this->user->user_id),
        )); 
        $this->load->view('footer_view');
}

but when I do reduce that code and take out the part that checks the user I am getting back an error saying that I'm trying to get property of a non-object. Obviously, that is talking about the user.

To try and fix this I have tried both

<?if(isset($user)){
  user header code in here
}else{
  non-user header code in here
}?>

and

<?if(!empty($user)){
  user header code in here
}else{
  non-user header code in here
}?>

both have returned back "Trying to get property of non-object" Any ideas would be much welcomed. I really don't like having excess code.

Was it helpful?

Solution

public function recentStories(){
    // default values
    $header_data = array('title' => 'Recent Stories');
    $storyList_data = array('query' => $this->data_model->pullAllStories());

    // extended data for logged user
    if (!empty($this->user)) {
        $header_data['user'] = $this->users_model->getUser($this->user->user_id);
        $storyList_data['admin'] = $this->admin_model->pulladminRecent();
        $storyList_data['user'] = $this->users_model->getUser($this->user->user_id);
    }

    // load your views
    $this->load->view('header_view', $header_data);
    $this->load->view('storyList_view', $storyList_data);
    $this->load->view('footer_view');
}

then in your views check "if (isset($user)) {" etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top