Search code examples
phpcodeigniterauthenticationcontrollers

CodeIgniter - Login form in a blog page


I am new in using codeIgniter, I am so sorry if i am asking a stupid easy question. I'm making a simple news website for my class project with the login form for the administrator in the same page (homepage) as the news posts (like 2 frames in html). Is it possible to load some functions and classes from different controllers and models in one view)? From many example that i learned, it always has only login page without other column with different controllers. And i tried to do what I think is true but everything i've tried just didn't work. If anyone know the solutions, explanations, or similar examples for this problem, please share me the explanation in any form (video, web link, document, anything). Thanks before..


Solution

  • If you need functions between controllers, you should define it as a CI model, or at least a helper. Controllers are simply interfaces between models and views. You need to create a login model, with commonly these functions:

    $user -> getUserInfo()
    $user -> login()
    $user -> logout()
    $user -> register()
    $user -> userExists()
    $user -> isLoggedIn()
    ..
    

    and then call these inside your controllers.

    Edit for comment: Yes you can include views inside your views, and this is like common usage.


    First method: directly in the controller

    $this->load->view("header");
    $this->load->view("subpage");
    $this->load->view("footer");
    

    and the result will be having three views appended together. Source: https://ellislab.com/codeigniter/user-guide/general/views.html


    Second method: in the controller assigning to variables

    $data = array();
    $data["header"] = $this->load->view("header", NULL, TRUE);
    $data["footer"] = $this->load->view("footer", NULL, TRUE);
    $this->load->view("subpage", $data);
    

    and in subpage view:

    <?php echo $header;?>
    (Sub page view data)
    <?php echo $footer;?>
    

    Third method: directly in the subpage view

    <?php $this->load->view('header');?>
    (subpage content)
    <?php $this->load->view('footer');?> 
    

    Sources for second and third: https://ellislab.com/forums/viewthread/88335/