⬆️ ⬇️

Separation of the view and controller

What is it?



I think many of the programmers understand the need to separate controllers (or, as they say, business logic) from the type (or display logic). This simplifies support, redesign and implementation of skins. There are a huge number of implementations of this outrage, and I would like to insert my five kopecks :)

You can find a huge number of topics on this topic, but mostly they relate to the "look", or rather all kinds of template engines. I will not talk about it now, otherwise it will turn into a regular holivar. The topic of this article is the controller, or rather, its implementation, although the display also applies to it :).

If somewhere there is something similar I will be grateful for the indication of the sources.



Why is this?



In addition to dividing the software system into modules, programmers are also interested in reusing code so as not to write the same thing twenty times and not use copy-paste, which leads to errors.

What do we have now?



Since pages very often have a lot in common, we often have something like:

 {? php include "header.php"?}
 some content
 {? php include "footer.php"?}


or

 header
 {? php include "{$ module} .php"?}
 footer


The disadvantages of the first approach are obvious, the second approach is not very good from the point of view of the controller.

Like this?



The idea is to use an object-oriented approach (since there is no function overload in PHP)

 {? php # main controller

 class Main_Cntr {
     function render () {
         # some logic
         include 'main.tpl';
     }
     function content () {
         # some logic
         include 'default_content.tpl';
     }
     function module () {
         # some logic
         include 'default_module.tpl';
     }
 };


Now in the whole main.tpl template main.tpl will be direct instructions:

 ... header ...
 {? php $ this-> content ()?}
 ... some view ...
 {? php $ this-> module ()?}
 ... footer ...


The advantage of the approach is that we can build a hierarchy of pages, giving rise to descendants:

 {? php # main controller

 class Other_Cntr extends Main_Cntr {
     function content () {
         # some other logic
         include 'other_content.tpl';
     }
 };


In this case, all the properties of the base view and the controller are inherited, you need to write a minimum of the code that directly relates to the changes, the controller and the view separately.

Conclusion



Naturally, implementation details can be changed, any template engine can be used here.

  1. The template code remains absolutely readable and understandable - the abstract layout of the logical elements of the page is used.

    The code of the controllers is also clear and simple, the creation of similar patterns and repeating elements on the pages is simplified.



    I will be glad to constructive comments and comments.

    PS: With some more or less signs, there were some glitches, I had to post curly braces.


')

Source: https://habr.com/ru/post/28317/



All Articles