To begin with, I'm a newbie, made just one small php projector in a "procedural" way, and now I'm looking for a way to jump over the huge gap that separates procedural programming from OOP. Most of the information on MVC provides that I have already moved across this abyss, and therefore practically useless. I have found the only article so far that explains as primitively as possible, starting with the basics, and I try to remake my project by analogy under MVC.

There was a problem - in my project the logic is a little more complicated than in the "business card site", and the article does not answer the question of where and how it is "more difficult" to implement.

In the mentioned article, the View class View extremely simple: the View->Generate($view_content, $view_template) makes require $view_template.'.php' , inside the template, in turn, the only place is require $view_content.'.php' .

Depending on the controller / method, I need to insert into the template in several places. The question is how to implement it correctly?

The solution I propose is to cram in constructions like include $block[0].'.php' into the main template (do not require it so that it doesn’t curse when there is no file), then fill this array with filenames in acc. models ...

Immediately there are questions:

  1. if I don't need to insert in this view, say, $block[0] - how will the include directive be executed? Will she try to access the file (in this case, NULL )? And in general, is it right to do so? ( upd: of course if (isset($block[0])) { include $block[0].'.php'; } - but it turns out that some of the logic is transferred to the form: /)

  2. How to deal with blocks that you want to use several times? For example, I have a simple mold there for filtering output from the base, but on one page should it filter suppliers, and on the other, goods in stock? To implement through a class?

    1 answer 1

    Read what widgets are and why they are. In short, this is an independent appeal to any module of your site, for example, a reviews module. You call the widget of this module, and everything is encapsulated inside it, it determines on which page (or in the parameter you pass it), knows which model it will apply to, and how to display the data in the template. Very comfortable and irreplaceable.

    From afar, your approach is correct, but I would implement an array by type

     $content = array( 'header' => 'Hellow world', 'short_description' => 'short description of page', 'footer_links' => array( 0 => array('name' => 'main page', 'link' => 'http://...'), .... ), ); 

    and this content is passed directly to the template template. Where I use it like this:

     <?php if (isset($content['header']) && !empty($content['header'])){ ?> <h1><?=$content['header']?></h1> <?} 

    and so on.