在MVC中查看,什么是布局以及如何创建布局 [英] View in MVC, what is a layout and how to create one

查看:90
本文介绍了在MVC中查看,什么是布局以及如何创建布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在视图中,我不了解布局是什么.之前,我曾在PHP模板问题上问过问题,但我仍然不太了解理解.我假设您为网站创建了一个通用布局,然后将每个特定的视图包括在该布局中....我想知道如何去做.此外,是否应该仅使用html来制作模板,因为我也查看了这些称为辅助程序的东西....我只是对MVC的查看"部分,实际模板及其制作方法感到困惑.通过例子我学得最好,如果你们有的话.

I dont understand what the layout is, in the view. I asked a question previously on the subject of templating in PHP, but I still dont quite understand. I assume that you create a general layout for the site and then include each specific view within that layout.... I would like to know how to go about doing this. Also, are should the templates be made using just html, because I also looked at these things called helpers.... I'm just confused on the View part of the MVC, and the actual templates and how they're made. I learn best with examples, If you guys have any.

还有一个更重要的问题,可以说我有一个用户只有在登录后才能看到的表格,我可以在视图中还是在控制器中对其进行控制?

Also, a more important question, lets say I had a form that a user saw only if he was logged in, would I control that in the view, or in the controller?

所以我会做

in the controller

include 'header';
if(isset($_SESSION['userID'])){
    include 'form';
}
include 'footer';

in the template

<html>
<?php if(isset($_SESSION['user_id'])): ?>
  <form>....</form>
<?php endif;?>
</html>

编辑

那么,布局中是否包含include语句以查看特定的视图模板?怎么会这样?

So, is there an include statement from within the layout to see the specific view template? how so?

推荐答案

仅出于围绕它的宗教热情,我犹豫要回答这个问题.

I hesitate to answer this question only because of the religious fervor that surrounds it.

要真正理解通用概念背后的问题,请参见此Wiki讨论页面这是有关Wiki MVC文章的讨论页面.

To get a really good understanding of the issues behind the general concepts see This Wiki Discussion Page This is the discussion page around the wiki MVC article.

这是我喜欢遵循的经验法则(顺便说一句,我使用CodeIgniter,听起来也像您一样):

Here is the rule of thumb I like to follow (BTW I use CodeIgniter and it kind of sounds like you are too):

视图"实际上应该没有逻辑.它仅应是HTML(在网络世界中),并带有简单地回显变量的胡椒粉PHP.在您的示例中,您将表单拆分成自己的视图,控制器将确定是否已加载该表单.

The "view" should have virtually no logic. It should only be HTML (in the web world) with peppered PHP that simply echos variables. In your example you would break out the form into its own view and the controller would determine if was loaded or not.

我喜欢这样看:视图不应该知道数据来自何处或去往何处.该模型应与视图无关.控制器对模型中的数据进行网格划分并将其提供给视图-然后它从视图中获取输入并将其过滤到模型中.

I like to look at it this way: The view should have no concept of where the data comes from or where it is going. The model should be view agnostic. The controller meshes data from the model and provides it to the view - and it takes input from the view and filters it to the model.

这是一个快速而肮脏的(未经测试-但应该可以理解)的示例:

Here is a quick and dirty (untested - but it should get the point across) example:

Theapp.php(应用控制器)

class Theapp extends Controller
{
   var $_authenticated;
   var $_user;
   var $_menu; // array of menus

   function __construct()
   {
      session_start();
      if (isset($_SESSION['authenticated']) && $_SESSION['authenticated'])
      {
           $this->_authenticated = $_SESSION['authenticated'];  // or some such thing
           $this->_user = $_SESSION['user'];
      }
      $this->_menu = array("Logout", "Help", "More");
      parent::__construct();
      $this->loadView("welcome"); // loads primary welcome view - but not necessarily a complete "html" page
   }


   function index()
   { 
      if (!$this->_authenticated) 
         $this->loadView("loginform");
      else
      {
         $viewData['menu'] = $this->_menu;
         $viewData['user'] = $this->_user;
         $this->loadView("menu", $viewData);
      }
   }


   function login()
   {
      /* code to authenticate user */
   }

   function Logout() { /* code to process Logout menu selection */ }
   function Help() { /* code to process Help menu selection */ }
   function More() { /* code to process More menu selection */ }
}

welcome.php

<h1> Welcome to this quick and dirty app!</h1>
All sorts of good HTML, javascript, etc would be put in here!

loginform.php

<form action"/Theapp/login" method="post">
   User: <input id='user' name='user'>
   Pass: <input id='pass' name='pass' type='password'>
   <input type='submit'>
</form>

menu.php

Hi <?= $user ?>!<br>
Here's your menu<br>
<? foreach ($menu as $option) { ?>
<div class='menuOption'><a href='/Theapp/<?=$option?>'><?=$option?></a></div>
<? } ?>

希望这会有所帮助.

这篇关于在MVC中查看,什么是布局以及如何创建布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆