Welcome Guest, Not a member yet? Register   Sign In
Best way to order class functions(poor title)
#1

[eluser]Kola[/eluser]
Hey, I've been slightly confused as how I should do this.

Basicly I have a controller called admin.php, this controller is checking if a user is logged in or not, and it controls which views are being shown.

So if I'm at http://root/index.php/admin/page/news/

it will load:

$this->load->view('news');

however, when I load the news page it loads an overview which I need a method for, so I thought: I'll just make a news.php controller and make a function display_news(){} with the appropiate method within that. However, these normally end up gathering data and passing it to a view, so in the end of display_news() it would say something like $this->load->view('news', $data);

Is my base structure wrong? as in is there a more sensible way to order these things. Or should I maybe create the news file as a helper (or maybe an overall admin_helper for various admin tasks) which I can load and then call display_news() in the view file?

Hope anyone got a few cents on this.

regards
#2

[eluser]toopay[/eluser]
Authentification, is a regular function which you may perform at most of your controller. Thats the main reason, why you need a separated "library" to handle that stuff, or create two "type" of your controller, which can be a global parent for the rest of your controller. In CI spec, the "softer" way to do that, is creating MY_Controller and specify those two (or more) types of controller, like...
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/* You can extend all of your controllers, which doesnt need authenticate user, from this class  */
class Public_Controller extends CI_Controller {
    
    public function __construct()
    {
        parent::__construct();
    }
}

/* Provides a base class for all controllers that must check user login status. */
class Private_Controller extends Public_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('some_auth_lib');
        // Make sure user logged in.
        if ( ! $this->some_auth_lib->logged_in())
        {
            redirect('login', 'refresh');
        }
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB