Achieving 3-tier architecture with Symfony PHP

无人久伴 提交于 2019-12-12 14:38:28

问题


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?


回答1:


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:


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.



来源:https://stackoverflow.com/questions/9484458/achieving-3-tier-architecture-with-symfony-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!