Hello. I don’t grow up to the PLO, I start practically from the beginning. The essence is such as it is more convenient or more correct to use variables in the concept of OOP, including in HTML templates? I know that frameworks can be peeped, but it can be all confusing because of their many classes, inheritances ... Examples: The simplest implementation of a template, via include. Data is passed from class.

<?php // Например, class User extends COntroller $data = ['var' => 'Exapmle']; $this->view->showView('home', $data); // В конструкторе Controller $this->view = new View(); 

Everything works in the view. Now there is a static method Error :: isError (). Here is the question: How can you "shove" View to automatically accept it? Passing to the template and writing the namespace in the HTML template home works. But every time there I would not want to register. A similar situation with the class User. How to check in the template that the user is logged in?

 // HTML код <?php if ((класс User->)isLogged()): ?> <a href="/profile/">Profile</a> <?php endif; ?> <?php if (Error::isError()): ?>Возникла ошибка.<?php endif; ?> 

Generally how correctly to turn encapsulation?

UPD: While I use the following:

 // В контроллере $data = ['error' => Error::show(), 'user' => $this->user->getUserData()]; $this->view->showView('user/sing_up', $data); // Во вью <?php if (!empty($error)): ?> <p style="color: #f00000;"><?php echo $error; ?></p> <?php endif; ?> <?php if ($user): ?> <a href="/profile/">Profile</a> <?php endif; ?> 

But, if someone advises another solution would be nice.

Helpers have not yet "passed", so in the future may do

  • From the simple one, put an instance of the User class in $data , and the view will be $ user-> isLogged (). Checking errors in the templates, I think not the best option. You can check for errors and inside the controller, and give a view with an error. Here the question is more architecture. The frameworks should be poked, well, or try for a start, familiarize yourself with the approaches. - Bookin
  • I have already written in the question what is easier to pass, but I would like to inherit. If you check in the controller, you need to use html, right? Again the wrong application. In frameworks it is difficult to understand what is where, because many things are taken into account there. They confuse how the simplest works .. - ozornick
  • What are you going to inherit from? Do you mean simply to connect in submissions? Well, connect no one bothers, it is a matter of choice, and how to continue to live with it. Why in the controller to use html for any checks? I don’t quite understand, I certainly don’t know what’s happening in isError, but for exceptions it’s worth using a global component that will catch exceptions and redirect to the error page. The frameworks are not as scary as you think, just try to figure it out. - Bookin
  • All the salt in writing your own) - ozornick
  • error is essentially an array with error points, then implode () and the output in View. Iserror - is there an array of errors, tru falls. With class user, I barely realized that I did not need to shove the whole class. Although if there are conditions for each group of users their own html, then you have to pass the variable to the View. And I wanted the User object or something else to be in View. For example, to select if the admin - bring the admin panel, if the user - in the profile, otherwise - Log in - ozornick

3 answers 3

If you do not want to pass the parameters in the view, but be able to request them from the view. I would do some kind of view helpers. Those. Because of the view you call a certain object that contains the necessary variables. And for example from anywhere in your code, fill this object.

    See, php was originally developed as a template engine. all these blotches of code in html in the spirit of <?= $variable ?> (instead of <?php echo ... ?> ), etc. - The legacy of those times.

    It can now be used in this way, but in large / medium projects it is rare. As a rule, template engines are used without introducing php-code.

    Compare Symfony 1/2: in Symfony 1, you could use templates as native php files, in Symfony 2, you usually use twig.

    Using template engines like twig has two goals:

    • completely remove the application logic from the view (novice programmer will not be able to insert a call to the template)

    • split designers and programmers (cheaper and faster)

    Thus, you have a question, not one, but three, therefore I will give three answers:

    1) You should not confuse the paradigm of building MVC applications (model-view-controller) and the OOP programming paradigm.

    2) You can easily use the MVC paradigm in a functional or procedural language, without using OOP.

    3) The template should not check whether the user has logged in, check the controller should either and either change the template for the view, or change the variables in the current view

      In PHP, like any other language, it is extremely dangerous to interfere with logic and display . For this there is MVC - its essence is simple, divide and conquer. All logic is processed in controllers, which transfer to the views some parameters, which can also be objects. But the logic and calculations of these parameters are encapsulated. Therefore - how you did everything: everything is correct.

      Transmitted objects can be ActiveRecord-derived from models (or they can be called the ORM layer) - and there is nothing wrong with their transfer, especially if the controller automatically locks them to write. Without a lock - ActiveRecord can be very easily rewritten into the database (but even without a lock on 800 views, no one did this to us).

      Next - view: there is the essence of the template. PHP is in itself an excellent templating engine. And the use of PHP (and not twig) for templates opens up excellent opportunities for templating:

       <? if ($this->error){ ?> <p style="color: #f00000;"><?=$error->toString()?></p> <? } ?> <? if ($this->user){ ?> <a href="<?=$user->getHref()?>"><?=$user->getTitle()?></a> <? } ?> 

      Namely, calling methods from objects passed into a view — which makes building views very flexible. Crazy typesetters do not try to connect to the database in the type code - even without a baton, neither contractors nor their own programmers - well, no one wants to be a complete deer in their profession.

      Using twig and others is either something imposed by the framework (for example, a symphony), or an act of extreme distrust to the creators of the species (layout makers, for example). So be aware - if you are a coder, and your company changes the layout of PHP-> twig - this is either a tyrant-managers, or a relation to the coders: like monkeys. The last paragraph - IMHO.

      • I am writing for myself alone, so if I continue to code, I’ll see what to change there. Now it is more important to understand the essence, and develop the logic of what and how to store, where to write And how to use it for display. Yes, an object can be deleted if you write something $ user-> delete (), but let it be solved by a copy of the object for View. - ozornick
      • @ozornick is possible, that's why I wrote "lock them for writing" - that is, for deletion as well. But in practice, no one wants to lose half the salary - we have 800 of these types, never in them so govnodoili (5 years). Although we did not do Lok before. At the extreme, the table will be restored from the dump. If we took someone who doesn’t understand anything in programming (to save money) - the problems of the authorities. - Alexander Goncharov