3
votes

In school web projects that I have done until now I have build my applications with a 3-tier approach. A data layer, an application and a presentation layer. I have used this architecture with python and java and I want to use it also with php. I was quite strict in the communication between layers as these were the requirements of the assignments and I can say that I find it quite good, as when I tried once to switch from mySQL to Mongo db, it was quite easy. I just had to change the code inside the function of my Data Access Object.

As I said before I want to write a bigger application now in php and I downloaded and installed the Symfony2 Framework, which is an MVC framework. My intentions is to test the application with MySQL initial and then switch and test it with Redis.

So myy question is: Can I use the functionality and all the "goodies" of the framework to write a 3 tier architecture application and how could I separate the layers?

2

2 Answers

3
votes

Symfony2 isn't an MVC framework per se, it's just a bunch of loosely coupled components working nicely together. There is a blog post about this from the lead developer.

You can choose how fine grained your bundles are, for example you can create a bundle with pure domain objects, business logic and interfaces for repositories, DAOs whatever you want. Optionally you can provide mapping configuration of your ORM of choice. Now you can create bundles for the data access strategies, like a bundle working with doctrine, other with redis, implementing your repository interfaces.

So you can go crazy about fine grained bundles structure, but for simple applications you can just put all of these under /src, or just create a bundle with all the data access strategies implemented there, and choosing one in your application with the bundles' configuration.

Studying other bundles' approaches also helps to get familiar with best practices, FOSCommentBundle or FOSUserBundle could be a good place to start.

2
votes

Sure. Trick is to wrap up your data layer (aka business objects) in services. Your application (aka controllers) interact with these services based on user input and pass the results onto the presentation layer.

Build your services carefully and you will be able to swap out the database layer without impacting your controllers or presentation.