Reputation: 975
So far, I create one controller for a site section with a method for each page - most are static pages, not requiring much logic or a model.
But where some of those pages are complex in functionality and need their own model, do I need to break them off into their own controllers? or is there a way to keep them in the one controller and load the model per method... probably the wrong thing to do
Upvotes: 0
Views: 993
Reputation: 782
its good to use specific controller for each page , so that the application becomes light weight, and also need to create its on model. So the application expand its functionality it becomes easy to develop :
class Location extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
public function index()
{
$data['title'] = "Hello world";
$this->load->view('locale_view', $data);
}
}
Upvotes: 0
Reputation: 102735
Totally agree with jondavidjohn's answer. I suggest just doing what works for you for now, and don't worry too much about overhead or doing the "correct" thing. You'll shortly realize what you need to do and how to be organized, and as far as overhead - Codeigniter is pretty lean, don't worry about optimization at this point - just get everything to work the way you want.
Take your first Codeigniter project and make it the best you can, but just consider it a throw-away app. Each time you work with it you'll learn more about how to use it, and especially if you keep reading and asking questions.
To answer your literal question: No, there's nothing wrong with loading the model per method. In fact, it can be "better" than loading in the __construct
of your Controller, because it ensures you only load exactly what you need. So don't worry about it.
So far, I create one controller for a site section with a method for each page - most are static pages
There's nothing wrong with this, but to make things easier, you can use the same method for each of your static pages, and keep your urls the same. Something like this:
class Pages extends CI_Controller {
public function __construct()
{
parent::__construct();
}
function index($page)
{
$this->load->view("pages/$page");
}
}
// config/routes.php
$route['page/(:any)'] = 'pages/index/$1';
This would map the url /page/my_first_page
to the Page controller and call index()
with the argument my_first_page
. Then you can use this for all your static pages without dynamic data. You can take this a lot further, but it's an example of one option you can choose to avoid writing a new method for every static page.
Upvotes: 3
Reputation: 62392
Because Codeigniter is so convention-less, it does lead to questions of opinion like this.
In general though I would always advise that more modularity is better than less modularity, and you can never over-organize.
Just because something isn't "wrong" doen't make it best.
Only you can decide in the end, because you will be the one to maintain it.
Upvotes: 3
Reputation: 56
You can load the model per method. It's not bad practice to do that.
$this->load->model('your_model');
Models typically aren't too intensive to load. You shouldn't worry too much about loading models unless you are trying to save ever single bite.
Upvotes: 0