Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reasons for not having a compact structure of an MVC application?

In a web MVC project, I have the following structure:

mymvc/                      -> Project root.
    public/                 -> Document root. The only one folder accessible from web.
        assets              -> Client-side assets. NOT ONLY for global themes and libraries, BUT ALSO for each specific "view" controlled by the "src/Application" components.
            css/
            js/
            images/
            ...
        index.php           -> Application's entry point.
    src/                    -> UI layer rules.
        Application/
            Controller/
            View/
            ViewModel/
        Dispatcher/         -> Application dispatching - route matching, dispatching to the specified controller, etc.
        ...                 -> Other classes used by the components in the "src/Application" folder.
    templates/              -> Layout and template files.

Note: All domain model components (entities, repositories, data mappers, services, etc) reside in a folder outside of mymvc directory, so that they can be accessible by any other application, too.

I thought - a lot, actually - about doing the following two steps:

  1. To move the templates directory to src/Application folder.
  2. To move the assets directory to src/Application, to create an alias /assets/ in the web server (Apache) configuration - pointing to the moved folder, and to allow full access from the external world to it.

The globally used assets - like css themes, or js libraries codes, or background images, or etc - could still remain located in the public directory - obviously not in a folder named or aliased assets.

I really find the two steps a very good idea, because, as I see it, both folders contain resources related to the structure from src/Application. So, instead of having something like this:

  • src/Application/Controller/AboutUs.php
  • public/assets/js/AboutUs.js
  • templates/AboutUs/[multiple-layout-and-template-files],

a structure like the following seems to be much better:

  • src/Application/Controller/AboutUs.php
  • src/Application/assets/js/AboutUs.js
  • src/Application/templates/AboutUs/[multiple-layout-and-template-files],

But, after I studied many web frameworks, I realised that all of them keep the two folders (templates and assets) completely separated from the application folder.

So I'd like to ask: Are there any specific reasons, why my proposed moving of the two directories can not, or should not be done?

Thank you.

like image 310
dakis Avatar asked Jul 25 '18 21:07

dakis


People also ask

What is App_Start folder MVC?

The App_Start folder of MVC application is used to contain the class files which are needed to be executed at the time the application starts. The classes like BundleConfig, FilterConfig, IdentityConfig, RouteConfig, and Startup. Auth etc. are stored within this folder.

What does a controller do in MVC architecture?

A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.

What is the name of the configuration files that the App_Start folder contains?

The App_Start folder contains class files executed when the application starts. Generally, all the configuration files like, e.g., Router Configuration, Filter Configuration, Bundle Configuration. MVC (V) includes FilterConfig. cs, BundleConfig.


1 Answers

It all depends on what you want to achieve, and what your workflow is. You seem to be working in PHP - it's worth looking at non-PHP frameworks like Ruby on Rails.

Typically, you want an output folder to be "read-only" - the developer should not manually edit files, instead the build and deploy pipelines run tools like Gradle to convert SASS/LESS and JS files (from the /source folder) into CSS and minified/concatenated Javascript and place them in the correct location in /public. The build and deploy pipelines often have different configs for development and production builds (minifying only for production, for instance).

In Ruby on Rails, the structure is mostly as you describe as "much better" - except that "templates" is a folder underneath "views" called "layouts". There's a build step (which runs automagically) which converts the various asset files (SASS/LESS, JS etc.).

You can see a detailed description of the Ruby on Rails directory structure here. Django's directory structure is explained here.

Answering the questions in your comments:

  • Where should the SASS/LESS/JS/Coffee script files go? - Up to you. In Rails, they live in /app/assets/javascripts and /app/assets/stylesheets; in these folders, there is one file for each view, as well as application-level files. This makes your build process nice and simple - you have just 2 folders to worry about, and don't have to modify your build every time you create a new view.
  • How do I structure my views - I have application-level views and specific page views. Again - mostly a question of convenience. In Rails, all the templates - live under /app/views. Application-level views live in a folder called /app/views/layouts - they are really not that different to the page-level templates, so moving them out of that folder doesn't seem to achieve very much, whereas keeping everything in the same top level folder makes build and configuration simpler.

So, no, there's no reason to do what you suggest.

like image 153
Neville Kuyt Avatar answered Sep 22 '22 00:09

Neville Kuyt