2

I'm familiar with OOP and PHP, but I'm new to MVC frameworks. I'm currently using Laravel and have found it wonderful so far, but things have gotten trickier as I've progressed, and now I realise I need to rethink my architecture.

The project is a digital assets manager for a small client, but I still want to make sure I'm developing it as well as I can, despite its size.

The basic object relationships are:

  • A PROJECT can have many SECTIONS.
  • A SECTION can have many ASSETS.

Seems simple enough, right?

I have PROJECT working fine, using REST, but I'm unsure how to deal with the SECTIONS and ASSETS.

There are 6 types of sections that a project can have ONE of: Eg. Photos, External Links, About, Videos, etc.

Some of the sections contains one or more asset. For example an About section can only contain one body of text, but a Photos section could contain many photos. Each type of asset has a different set of properties.

Ideas for approach

If I was writing this using vanilla PHP, I'd probably have an Asset object and then have Photo, About, External Link, that Extended Asset.

The more I've thought about it, the more I've questioned if I even need a Section object, too.

Given that I'm using MVC, I'm not sure whether I should have a different Controller and Model for each Asset type, or if that's overkill. Maybe I should just have an Asset Model (and one Asset Controller), and then have more Models for each of the Asset types that Extends that Model.

Any assistance to help shape my thinking toward a good use of MVC would be greatly appreciated.

Update: Further thoughts

Having had more time to ruminate, learning more about MVC and think about comments made by @RobertHarvey, I've started thinking more about the Models I've chosen. I come to think that's really where this question (and its solution) lies.

I've started building the site with a model for each SECTION type and ASSET type (eg. VideoSection and VideoAsset are two models I have now.

A Project can have one VideoSection. A VideoSection can have many VideoAssets. Fingers crossed this is a good way to approach this.

  • Are you looking for answers specific to Laravel or more abstract about using the MVC design/architecture pattern? – Jeremy Jun 14 '17 at 13:47
  • @Jeremy The answers do not have to be specific to Laravel. It uses an MVC pattern, so I can figure the Laravel part out for myself. – Django Reinhardt Jun 14 '17 at 13:51
  • What does your domain design have to do with MVC anyway? They seem like orthogonal concerns to me. – Robert Harvey Jun 14 '17 at 22:37
  • 1
    @RobertHarvey If you have a more appropriate answer, please share it. Thanks! – Django Reinhardt Jun 16 '17 at 10:22
  • 1
    I saw your edit. Consider thinking about organizing your controllers in terms of the operations you want to perform, rather than the structure of your data. – Robert Harvey Jun 16 '17 at 16:07
1

This is very subjective and abstract, but the following approach has helped me in the past.

When ever I'm presented with design challenges like this and I'm using an MVC style design pattern. I find it helpful to focus on 3 questions about how the application will handle state. For me, decisions about handling behaviours based on the state of my data tends to naturally slot things into place most of the time.

1) Model "things":

Does this thing have a state? IF YES: consider creating 1 or more model objects for this "thing".

2) Controller "things":

Do I care about this thing changing state? IF YES: consider creating a controller to handle behaviours in response to change to the state of this "thing".

3) View "things":

Do I need to show the state of this thing? IF YES: consider creating one or more views to display the current state of this "thing".

Obviously this is very simplistic, but that's the point.

You can just keep asking the same questions as you expand the scope of your application and refine the behaviours you need to handle.

I also find it useful to maintain some strict boundaries to help keep things loosely coupled and single purpose:

1) Only manipulate views through a controller.

2) Do not couple model objects to views.

3) Do not couple model objects to controllers, use an interface instead.

3) (Data read) Only update the model data from a service layer.

4) (Data write) If you must write data to the model from user input and persist it, decide on a consistent approach and stick to it.

5) Everything needs to communicate, so decide on a consistent approach and stick to it.

Its all pretty subjective as I said and just one view on things. I dont imagine everyone will agree with me, but hopefully that helps you make some decisions.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.