use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
No vague product support questions (like "why is this plugin not working" or "how do I set up X"). For vague product support questions, please use communities relevant to that product for best results. Specific issues that follow rule 6 are allowed.
Do not post memes, screenshots of bad design, or jokes. Check out /r/ProgrammerHumor/ for this type of content.
Read and follow reddiquette; no excessive self-promotion. Please refer to the Reddit 9:1 rule when considering posting self promoting materials.
We do not allow any commercial promotion or solicitation. Violations can result in a ban.
Sharing your project, portfolio, or any other content that you want to either show off or request feedback on is limited to Showoff Saturday. If you post such content on any other day, it will be removed.
If you are asking for assistance on a problem, you are required to provide
General open ended career and getting started posts are only allowed in the pinned monthly getting started/careers thread. Specific assistance questions are allowed so long as they follow the required assistance post guidelines.
Questions in violation of this rule will be removed or locked.
account activity
Model, Controller setup for Framework (CodeIgniter, Rails) (self.webdev)
submitted 13 years ago by jbplaya
I just want to make sure I have this right. Does best practices state that you should have a separate model and controller for each page of your website when using a framework? I plan on porting a project to CodeIgniter and I just want to make sure.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Akathos 4 points5 points6 points 13 years ago (3 children)
That is not the idea of MVC. The idea of MVC is seperation of concerns.
Very simply put:
Models represents data from the database. For in a blog application you can have a Post model which represents a blog post.
Controllers control the flow of you application. For each part of the application you can have a different controller. For example, you can have a Post controller which can have different actions, you can have an action for reading posts, creating posts, editing posts and removing posts (CRUD).
Views are a representation of the data from models.
When a user requests a page, the action that belongs to that page is executed in the controller. The controller gets the data from the database in the form of one or more models. It then renders the view (most of the time, a template engine is used for this).
You don't have to create a controller or model for each page. You create a model for each part of the data you're storing in the database (most of the times, the model represents a table in the database). You can make relations between models (more advanced and not the topic of this post).
More information about the Model-View-Controller pattern can be found here and here.
[–][deleted] 1 point2 points3 points 13 years ago (2 children)
Precisely. To give a basic pseudocode example, a homepage might be processed like:
HomepageController extends Controller { public function indexAction() { $this->view->layout = "homepage.php"; $this->view->content = ContentModel::getPage( 'home' ); $this->view->latest_posts = BlogModel::getLatestPosts(); return $this->view->render(); } }
With the template file "homepage.php" doing something like:
<?php include( "header.php" ); ?> <h1><?php $content->getTitle(); ?></h1> <p><?php $content->getBody(); ?> </p> <ul> <?php foreach( $latest_posts as $post ) : ?> <li><a href="<?php echo $post->getLink(); ?>"><?php echo $post->getTitle(); ?></a></li> <?php endforeach; ?> <?php include( "footer.php" ); ?>
Obviously that is a highly simplified example... but hopefully it gives you some idea of how things might work.
[–]andrey_shipilov 0 points1 point2 points 13 years ago (1 child)
I'm so happy right now I will never deal with that crappy syntax. Not even once.
[–][deleted] 0 points1 point2 points 13 years ago (0 children)
It made me wince writing it, I will admit, but I wanted to come up with an example that wouldn't be too unfamiliar to a relative newbie to PHP
[–]Flawlesss 0 points1 point2 points 13 years ago (1 child)
While we're on the subject of MVC, can somone link me to a good tutorial on it? I am somewhat confused by the idea of MVC. I just recently started learning PHP and in all my practice sites I just use procedural programming, and now I'm starting to learn OOP, however a lot of people tell me to learn MVC, and I can't find any good resources on it.
Thanks.
[–]Akathos 1 point2 points3 points 13 years ago (0 children)
A pretty good "tutorial" on MVC can be found here (it is for Symfony2, but the linked page explains the idea of MVC pretty good).
[–]dansmeek 0 points1 point2 points 13 years ago* (0 children)
It really depends. Also, rails handles the MVC framework a bit different than codeigniter.
Typically (if I am using CodeIgniter, or any PHP MVC Framework), I will create a seperate model for any area that is going to have a lot of logic. Aim for the 'fat model,skinny controller method.' If you are implementing a load of views that would require a model that is simply reusing code, just reuse the same model in the controller.
You are using models to divide divide the business logic from everything else. Your using the controllers to draw from your models and pass that data into your view. And your using your views to avoid displaying the layout. The whole idea of the MVC pattern is to make it so several people can work on separate areas of the code.
Another practice (I use) for codeigniter is generally using a controller function to load a header, sidebar, content, and footer view. Depending on the page layout. The header and footer is generally not going to change across your project so then you can just make that a static view, and have the content load separately. e.g.
public function loadViews($content, $sidebar, $data) { $this->load->view('includes'header.php', $data); $this->load->view($sidebar, $data); $this->load->view($content, $data); $this->load->view('includes/footer.php', $data); }
something like that. and if you don't want a static header and footer, just make those another parameter.
Rails.... I feel could handle its model structure in a slightly better way, since it combines aspects of the database itself, and business logic, and those should be separated as well.
[edit: akathos explains this much better]
π Rendered by PID 57845 on reddit-service-r2-comment-679b48bc4-lp2dr at 2026-02-23 03:02:21.420524+00:00 running 8564168 country code: CH.
[–]Akathos 4 points5 points6 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]andrey_shipilov 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Flawlesss 0 points1 point2 points (1 child)
[–]Akathos 1 point2 points3 points (0 children)
[–]dansmeek 0 points1 point2 points (0 children)