I've been learning MVC for the past week, specifically, the Laravel PHP framework. I've signed up for Laracasts, and I have a basic understanding of how the MVC pattern works, but one thing which has not been explained to me is just when to use a Model.
Every time I've been demonstrated what a model is, it has extended Eloquent
and linked to a database table. Jeffrey Way has specifically noted not to conflate "model" with "table", so I'm assuming you can create and use models that aren't linked in to a database table, but I'm unsure of when to do so.
Moreover, I'm not entirely certain about this: When should I use a model, and when should I use a helper class/library? The Laravel documentation does not cover models in much detail, and only talks about Eloquent mainly.
I can think of two examples where I'm uncertain of where my code should be placed:
A navigation creator
Say I have a set of articles delivered by an Article extends Eloquent
model, linked in to a articles
database table. What happens if I want to create a set of dynamic navigation links to other pages, such as the previous
article, the next
article, an article nearby
, and a similar
article.
This would serve a pretty large and crucial component of a site. I could imagine each of the four previous code-highlighted examples being a method of a class, but should that class itself be an independent Model, or merely a helper class of Article
?
An image builder
Say each article has images embedded inside them in some easy-to-write form. I could write an ImageBuilder
class which could parse each article and replace my embedded user-friendly image syntax with complex HTML based on the type of image and what position it should have in the document.
This seems highly dependent on the Article
class itself, so I'd be leaning towards it being a helper class rather than a model in itself, but I'm still not sure.
Where is the distinction? Where do I draw the line? Specifically with my examples, what would you recommend?