Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to structure MVC application composer style?

I'm in the very early stages of making an MVC web application. I want to try and do things composer style. Here is my directory structure so far:

public_html
    |-vendor
    |   |-MyVendorName
    |   |   |-DomainObjectClass.php

So that's where I am storing domain objects.

I'm also trying to do MVC as close as I can to the way it is done in the answer to this question

Where I am a bit stuck is where to put the Model, Controller, View, Services, DataMappers etc. Should I make subdirectories of MyVendorName (eg MyVendoreName/DomainObjects/DomainObjectClass.php and MyVendorName/Services/SomeServiceClass.php etc) or would it be wiser to make a directory separate from vendor called classes or src or something and do MVC stuff there?

Edit: Everyone is saying that vendor is for third party libs, I get that. But the way I am writing my domain objects is very decoupled from the MVC side of things. In fact, they do not even know they are part of an MVC app. They could very easily be reused in other projects (I intend to do this). So it seems illogical to me to put it in src/ or app/

like image 463
Cameron Ball Avatar asked Sep 30 '22 14:09

Cameron Ball


2 Answers

This is an extremely debatable topic and there's no one right answer. However, I'd dispense the following hints:

  • the vendor directory is for third party dependencies, you should not be writing your own code in it
  • put your own code in a src or lib directory, next to the vendor directory
  • neither of these directories should be in the public webroot folder; the webroot should be a separate directory containing only publicly served files like CSS and JS files, anything else is outside the webroot
  • structure your class names in namespaces
  • from the namespaces follows the directory structure
  • explicitly having a MyVendorName\Controller, MyVendorName\Model etc. makes sense
  • structure as deeply as makes sense, e.g. MyVendorName\Model\DomainObjects\Foobar\Subclass makes sense
like image 66
deceze Avatar answered Oct 04 '22 21:10

deceze


I would suggest this approach:

project_dir
    |-vendor
    |    -(vendor directories, installed via composer)
    |-public_html (images, javascript, css, html files/angular views)
    |-app

Inside of your app directory is where you put your PHP code. Inside of there you can organize your controllers, services, data mappers however you like, but this structure provides a strict separation between what should be accessible from the outside (public_html) and what should only be executed by apache or cli (everything else).

As @deceze said, though, besides breaking out the vendor directory and the public directory, how you organize your application code is wholly up to you and should match whatever is most appropriate for the task you're attempting to accomplish.

like image 20
Jeff Lambert Avatar answered Oct 04 '22 19:10

Jeff Lambert