Layered PHP in the right direction?

I have a specific question that can use a general answer ... When creating multi-level applications in PHP, everything must be done at the level of business logic or any layer can work ... Example: Suppose I create an application that displays user information (from databases) at the presentation level. Do I have to use the business layer to simply transfer data to the presentation layer or just to get information from the database directly at the presentation layer. Should the presentation layer be used by JUST to represent data, the access level used by JUST to receive data, and all work must be done at the business level?

In addition, speaking of different layers, it is best to do something procedurally or use OOP (for example, use includes displaying templates and using a class to include templates, checking data procedurally or using a class or functions vs classes to get data from the database data, etc.)

As you can see, I'm trying to understand how everything works, and the best way to do something. However, if you have any advice or any general advice on this ... please leave them.

thanks

+4
source share
7 answers

Web applications and the N-level are interesting, mainly because the concept of the N-level has expanded due to the widespread adoption of JSON and AJAX, or Flash and XMLRPC. This diagram in Webopedia displays a checkerboard blue line that expresses it well. To summarize: the logic of your company, accessories and presentations can not only exist on the server, but in some cases right in the browser. However, the N-level intent is separability. If you need to change your user interface or database or add other user interfaces, you should not influence your business logic. And this is what your API will define - expecting your HTML and CSS to be dropped for Flex and MySQL to be changed for Oracle.

These requirements are defined, and in some web applications that I used, N-level variations are used simultaneously. Take, for example, LyrisHQ (mail ASP). They have a client web application. Recently, they have been staring at pushing their Flash-based application. Obviously, this is delivering large amounts of data directly to the browser, and there is probably a bit of business logic duplicated in the Flash interface. However, they must support both applications, since each of them is necessary for different (and overlapping) requirements.

Most common PHP applications do not consider delivering large amounts of raw data to the browser. But if you were, it will tell you very quickly how you would like to design your APIs. Most likely, you will need controllers that could discuss XMLRPC, REST, or SOAP ... in addition to the similar internal controller class that used your PHP presentation templates. This will strictly mean for a simple login page, you will have a PHP template for the login form that spoke to the LoginController class. The XML interface will also use the same LoginController class. Just like you thought you would be scared of writing SQL in an Ajax request ... you strictly avoid writing queries in your presentation templates.

Business levels can be more or less strict because often there is no need to switch database brands. In a strict N-level design, how your business objects will talk to your database, you would like to switch from MySQL to MS SQL without overwriting the Business level. Sometimes this is done by modeling objects for each table (Table Gateway), each row (active record), each connection, or each transaction. This is what is useful for PDO or PHP-ADO, but not enough for complete isolation. Java ORM / Persistence levels such as Hibernate demonstrate better isolation, often providing an object query language (OQL).

I myself am currently implementing a back-end transition from a MySQL-based PHP application to MS-SQL. The application has always used direct SQL queries. Imagine how to select a series of queries in a class and either abstract them or subclass them, and hopefully not change the business logic. At a minimum, you will want to make all of your SQL calls indirect. ( SO post in PHP ORM .)

And finally, to your question about OOP: use it as you must fulfill your requirements. My personal technique is to start with the logic right in the PHP view template in a few minutes to make the ball roll, pretty soon I will reorganize it into a class and a template. If I have common ideas, I go out of routines into shared classes, trying to preserve the DNRY principle. (A SO post is here. OOP is not a fundamental requirement for N-level design. DNRY is very important for keeping your code in service, because often the deadlines and scope of -shift destroy the API. Refactor it until you get something what you need to continue. I'm sure OOP will help you there.

+3
source

I once read something saying that large models (business layer) should be preferable than large controllers (access layer).

Also: it really depends on the project. In my eyes, it does not always make sense to use OOP for everything (maybe not everyone will agree here), especially in scripting languages ​​such as PHP, it is often easier to just make validators as functions ...

+2
source

I definitely advise you not to have database calls in the presentation layer to defeat its purpose.

There are different approaches to multi-level architecture , and they have different recommendations.

The Zend Framework I worked with usually takes the form of a Fat model / Thin controller.

If you find this article Notes on choosing the PHP Framework , it will be pretty good when comparing (in this case) CakePHP with the Zend Framework.

The biggest difference in my opionion is the consortium-based approach compared to CakePHP, which is very different from the configuration focus in the Zend Framework.

Using a framework that makes a lot of sense when implementing a multi-level architecture, you are forced to some extent, or at least click on the use of OOP, which is not so bad.

Of course, you can implement, say, a three-tier architecture with pure functional challenges, but it will not scale as well based on my experience.

For a website, the MVC template, which is actually very different due to the way the levels are connected, is a good choice.

The difference between a “normal” three-level arc and the MVC pattern is that the view has access to both the controller and the model layer, where, since the view level only has access to the logical level of the three-level arch level.

+1
source

I agree with what Franz said, especially regarding larger models over larger controllers. You can also use a rule of thumb to help distinguish where the code should go: if it has anything to do with the structure / stream of the user interface, this is in the controller; if this is pure business logic, it goes into the model.

I will add that as a Java developer who had a lot of experience with Struts, before testing the waters in PHP, it took me a while to figure out how to apply MVC ideas to the scripting language. I personally found that using a system like CodeIgnitor helped a lot. It provides a framework like Struts and encourages good MVC templates.

0
source

My world looks like this:

DB1 <- Model1 to provide access functions, data integrity, business rules for this data set DB2 <- Model2 to provide access functions, data integrity, business rules for this data set

Controller: controls the flow of data for displays, filters / organizes data from Views. Implements business rules for this application. Knows business rules between Models.

Views: nothing but templates that display data provided by the Controller. Transfer all data to the controller. Does not know Model1 or Model2 or the business logic that regulates them.

0
source

I recommend doing some research on design patterns, as this is the missing piece of your puzzle. There are a number of PHP-specific books that cover this topic (good ones from APress), as well as a “bible” of design patterns: “Design Patterns: Elements of Reusable Object-Oriented Software”

0
source

Wait for a more stable version of Doctrine2 .

It develops with the philosophy of subject objects with incompatible heritage. Using this framework will make it much easier to develop a layered application.

0
source

All Articles