Answer the question
In order to leave comments, you need to log in
Question about View (PHP MVC)?
Greetings! I re-read several articles on MVC, drove into the essence. But with View it's still not entirely clear. So, I run an application from index.php, the route method parses the user's request and gives control to one of the methods of the corresponding controller. And in this method, the creation of a model and a view, as well as the transfer of data from the first to the second, is already taking place. As a culmination $view->render('template_name'). But here's what happens. I have, for example, a BlogController controller. It has two methods - indexAction, which simply returns entries from the blog, and addAction, which displays the form for adding a blog entry and processes it. Accordingly, I need to use two different templates. And here it is, the question - for each action I need to create a separate template, in which variables will be substituted? So it turns out impractical, if I have 5 controllers with 5 actions in each, it turns out to be 25 templates. Or is this normal practice? What if I want to change the site header a bit, do I have to change all 25 templates?
Answer the question
In order to leave comments, you need to log in
Yii has such a concept as layout. It is the layout used in most of the site's pages. Files with differences are inserted inside it (in fact, this is the view). Thus, the layout is made up once, and the differences that are specific to each page are placed in separate files. If, for example, we are talking about a form, then you can write 2 such files - with the markup of the form and with the results of its processing. How these files are generated - with or without template engines, it does not matter.
I join. There should be no copy-paste, common fragments in the view are placed in separate included files. in CakePHP this structure is called elements book.cakephp.org/1.2/en/view/97/Elements other frameworks have their counterparts
Who's stopping you from moving the common parts (header, footer, sidebar) into separate subtemplates and including them? Or, like in jung, you can use template inheritance, make a basic template and expand it little by little. Truth. you will have to write the inheritance implementation yourself, since those template engines, like Twig, that support it, in my opinion, are suboptimal and crooked.
Also, the question is, who forbids using (if required) one template for different actions? Nobody forbids.
In any case, writing 2 times / copy-pasting the code is wrong.
You will not believe it, but sometimes there can be a dozen views for one request. The simplest example is a different type of returned data (xml, html, json, etc.)
Вообще говоря, да, для каждого действия нужен свой вид/шаблон, причём вид и шаблон считать синонимами можно только с натяжкой. Или даже несколько видов, среди которых контроллер выбирает нужный в данный момент.
Использования наследования и включений шаблонов это следование другим практикам и паттернам непосредственно к MVC отношения не имеющим. Как, кстати, не имеет отношения к MVC и «запускается приложение из index.php, метод route разбирает запрос пользователя и отдает управление одному из методов соответствующего контроллера».
When choosing between inheritance/decoration and inclusion, I would advise you to focus on the former. Even without the use of templating engines like Smarty or Twig, this is pretty easy to do with ob_* functions, especially if two-level is enough. The render method might look something like this:
ob_start();
require $template;
$content = ob_get_clean();
require 'layout.php';
<html>
<body>
<div id="header">Шапка</div>
<div id="content">
<?= $content ?>
</div>
</body>
</html>
<?php foreach($posts as $post): ?>
<div class="post">
<h1><?= $post->title ?></h1>
<?= $post->content ?>
</div>
<?php endforeach ?>
IMHO it would be nice to adopt REST , it will look something like this:
GET /items #=> index
GET /items/1 #=> show
GET /items/new #=> new
GET /items/1/edit #=> edit
PUT /items/1 #=> update
POST /items #=> create
DELETE /items/1 #=> destroy
Accordingly, you can use the same template for new and edit, and depending on the type of request, a different method is triggered in the controller.
Yes, and they said correctly about layouts - you need to use it, code duplication is evil.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question