Reputation: 613
I'm building a website and I haven't done MVC before, but I feel like I'm understanding it (hopefully).
I have controllers, and models, and views for different aspects of the website, like register has its own controller, model, and view.
The website has a login panel on the main page, and account management panel, on the main page.
Thus, through the index controller, I have loaded a login model, and accountManagement model, through this one controller.
I don't know of any other ways to do it - without duplicating functionality - since these are all displayed on the main page, depending on whether the user has various cookies.
I was wondering about the coding practice when it comes to this (multiple models loaded in one controller), and possible suggestions, if it is a bad practice.
There are existing threads on "how can i load two models in one controller", and so on, however, my question is specifically about the coding practice of this, and I couldn't find a question for that.
Upvotes: 1
Views: 1277
Reputation: 3158
there is no problem with loading more then one model in a controller. if you need the model in all the methods in the controller then load it in the constructor per learn's post. if the model is just needed in one method you can load it in the method.
as you build up code in your controller - refactor it into your models. this makes the code in the controller abstract. in other words the less "specific" the code is in the controller the less it will need to change.
if you are doing a login and then checking the login status on every page - the easiest is to put that check in the constructor so then you are covered for all the methods in the controller. another suggestion is to always return something from the login check. you can return an object or array that has the users name, email, etc. So then instead of checking the login status on your controller - you are checking if anything was returned from your login checking methods. this it makes it much more flexible as you add features.
public function __construct() {
parent::__construct();
$this->load->model( 'logingate' );
$this->load->model( 'accountManagement' );
if ( ! $this->admin = $this->logingate->returnAdmin() ) {
redirect( '/login/', 'refresh' ); }
} //
so then $this->admin is immediately available to any of your methods in the controller, called models, and any called views.
echo 'Hello ' . $this->admin->name ;
Upvotes: 2
Reputation: 300
You could do something like this when loading models into a controller:
function __construct(){
parent::__construct();
$this->load->model('user_model');
$this->load->model('post_model');
}
function index(){
$user_id = $this->session->userdata('id');
$data['user_details'] = $this->user_model->get_user($user_id);
$data['posts'] = $this->post_model->get_latest_posts();
$this->load->view('homepage_view', $data);
}
Hope this is what you are looking for. If it isn't just comment and I'll try help out.
Upvotes: 0
Reputation: 23836
I believe this is what you looking for TankAuth. Tank Auth is an authentication library for PHP-framework CodeIgniter. It's based on DX Auth, although the code was seriously reworked.
Its simple to configure with CodeIgniter and its code is well formatted.
You can learn many things here.
Upvotes: 0