jump to navigation

A Sample MVC App Build on Top of Symfony2 and Laravel4 Components (Part-1 Introduction) June 20, 2014

Posted by Tournas Dimitrios in sample-MVC.

Word Cloud SymfonyThe Model-View-Controller pattern (MVC) is a methodology for separating the concerns of an application’s domain, presentation, and user input into separate (specialized) components. The days where HTML code was intermixed with server-side code to achieve dynamic web-applications are just ancient history (in terms of Internet technology). PHP itself works like a template engine. However, when used irresponsibly, it leads to very ugly and un-maintainable code (not to mention the environment for teamwork). Within the context of web- development environments, MVC is the de facto architectural design pattern, not the only though .
Many PHP frameworks nowadays provide an “out of the box  MVC skeleton” as starting point, a developer has to make a careful plan before he/she decide which framework to choose for the intended project .

This article will present a fully functional MVC application build from components taken from two well known PHP frameworks (ie Symfony2 and Laravel4). The final result is a simple “User list” where users can be registered / updated and deleted from the database (basic CRUD operations) . Not a very useful practical example, one might say. Yes indeed, this simple example might not have a practical implementation. But it’s the best way (IMO) to demonstrate the core concepts of a well structured web application. All frameworks are doing the exact same things to achieve similar functionality, in a more efficient way of course. A lot of Bootstrapping is done behind the scenes until a framework’ skeleton application is ready to respond on requests made by a browser. Just for indicative purposes, Symfony2 / Zend Framework2 are instantiating more than 200 Classes at bootstrap. Many more classes are lazy loaded “just the time” their functionality is required  by using Dependency Injection. Even more, all instantiated Classes are kept as services in a container (a bucket so to speak) for later use (might the application demand their services). The practical example of this article intentionally refrained to use a Dependency Injection Container (DI). Firstly, for simple web applications (as this one) there is no  benefit to make use of it. Secondly, the reader will get a better overview how the puzzle is structured, as everything is on front and nothing is passed over to background functionality (which is the case if we had to use a DI container). An obvious question that might arise is if this code could be used on production. Although my answer is definitely Yes, I will conclude with :  “just because we can doesn’t mean we should“. There are so many frameworks to choose from, this fact makes this code-base not the best choice for a production application that might be extended with extra functionality in the future .
This Blog has already started to publish an article series about Silex. That framework (Silex) can be used as a foundation for all kind of web applications (from prototyping and small web apps up to high demanding web services and API’s) . Of course there are so many alternate solutions out there which could be an excellent choice for our next web application.

Prerequisites : I assume that the reader has set up a development box (LAMP stack) on his local machine (or a Cloud based development environment). Having a familiarity with the terminal and basic skills of Linux administration is a must (defining virtual hosts , configuring local dns , changing permissions of folders). As the project is hosted on Github, basic skills with a Git-client will be proven helpful (cloning the project from Github) . If you find any bugs or want to contribute to the this GitHub repository, don’t hesitate to PR you changes to GitHub (I will gladly accept your contribution).
The reader should also have a good grasp of OOP concepts. Having basic knowledge of design-patterns will be proven helpful to fully understand core concepts of this project.

Overview of basic features of this MVC app :

  • The request/response life cycle is handled by Symfony’s HttpFoundation Component
  • Validation and filtering of submitted form data is done with Symfony’s Validation component
  • Csrf protection (cross site request forgery) is achieved with a custom class .
  • Symfony’s Yaml component : Configuration files of this project are written in Yaml, it’s a human friendly data serialization format. Yaml is used extensively by the Symfony framework (for all its configuration files).
  • ORM (object relation mapping) is used to access the database (db abstraction). Laravel’s ORM (Eloquent) is a simple Active-Record design pattern implementation. I have chosen this component (library) because it has minimum configurations (Db credentials of course) and there is no need to even write one single line of SQL-code while doing database related tasks.
  • Twig is used as template engine
  • Pure is used as a Responsive CSS library (which has a minimal footprint, its size is only 18K)
  • Jquery for client side notifications (the user is notified for create/update/delete actions and when a Csrf attack is detected) .

The final outcome of the project is printed on the screenshot below :

A screenshot of the final result


Final thoughts (of this first part) :

Each framework has its own strengths and weaknesses, it’s up to us to explore those features . No book or article  could ever print all details of any framework, this can only be achieved by digging into the source code and of course by experimenting . Don’t be afraid to mix different code bases, most likely , the final outcome will be useless for production, but the real gain is that you’ll get a deep insight about the code you are playing with. This article is the outcome of my “play time”, most likely the result won’t ever be used on production but my gain is that I have now a better understanding of “how things work behind the scenes”. Symfony’s components are used by many frameworks (ie Laravel, Drupal, Silex …..) , so the knowledge gained from this project is 100% valid in other frameworks to. I have learned something new this weekend (while developing this project) , hope it will be proven beneficial for you as well. See you on the next part of this article, that part will dig into the actual code . For now, visit my GitHub repository , explore the code by yourself, and why not, commit back any improvements you might have made on the code. I will gladly accept your contribution.



No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s