手把手编写PHP框架 深入了解MVC运行流程

2019-05-03 01:54:11王振洲

4.4 部署视图 

在 views 目录下新建 header.php 和 footer.php 两个页头页脚模板,内容如下。 

header.php,内容:

 <html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title><?php echo $title ?></title>
  <style>
    .item {
      width:400px;
    }
 
    input {
      color:#222222;
      font-family:georgia,times;
      font-size:24px;
      font-weight:normal;
      line-height:1.2em;
      color:black;
    }
 
    a {
      color:blue;
      font-family:georgia,times;
      font-size:20px;
      font-weight:normal;
      line-height:1.2em;
      text-decoration:none;
     }
 
    a:hover {
      text-decoration:underline;
    }

    h1 {
      color:#000000;
      font-size:41px;
      letter-spacing:-2px;
      line-height:1em;
      font-family:helvetica,arial,sans-serif;
      border-bottom:1px dotted #cccccc;
    }
 
    h2 {
      color:#000000;
      font-size:34px;
      letter-spacing:-2px;
      line-height:1em;
      font-family:helvetica,arial,sans-serif;
    }
  </style>
</head>
<body>
  <h1><?php echo $title ?></h1>
 
footer.php,内容:
 </body>
</html>

然后,在 views/item 创建以下几个视图文件。 

index.php,浏览数据库内 item 表的所有记录,内容:

 <form action="<?php echo APP_URL ?>/item/add" method="post">
  <input type="text" value="点击添加" onclick="this.value=''" name="value">
  <input type="submit" value="添加">
</form>
<br/><br/>

<?php $number = 0?>
 
<?php foreach ($items as $item): ?>
  <a class="big" href="<?php echo APP_URL ?>/item/view/<?php echo $item['id'] ?>" title="点击修改">
    <span class="item">
      <?php echo ++$number ?>
      <?php echo $item['item_name'] ?>
    </span>
  </a>
  ----
  <a class="big" href="<?php echo APP_URL ?>/item/delete/<?php echo $item['id']?>">删除</a>
<br/>
<?php endforeach ?>

add.php,添加记录,内容:
 <a class="big" href="<?php echo APP_URL ?>/item/index">成功添加<?php echo $count ?>条记录,点击返回</a> 

view.php,查看单条记录,内容:

 <form action="<?php echo APP_URL ?>/item/update" method="post">
  <input type="text" name="value" value="<?php echo $item['item_name'] ?>">
  <input type="hidden" name="id" value="<?php echo $item['id'] ?>">
  <input type="submit" value="修改">
</form>

<a class="big" href="<?php echo APP_URL ?>/item/index">返回</a>

								 
			 
相关文章 大家在看