PHP中HTML头的最佳实践 [英] Best practice for HTML head in PHP

查看:63
本文介绍了PHP中HTML头的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个非常简单的页面,有一些更复杂的后端。我一直这样做,但我觉得这是不对的。但我还没有对我产生太大的帮助。

我有我的index.php,header.php和function.php。 index.php包含header.php,它从function.php中调用一个函数。我不确定的主要原因是如何使网站具有足够的动态性,以便可以轻松编辑页面,而且易于编辑也是它需要编辑的重要部分。

index.php

 <?php 
session_start();
include_once'header.php';
?>
//页面内容
< / body>
< / html>

header.php

 <!DOCTYPE HTML> 
< html lan =en>
< head>
< meta charset =utf-8/>
< link rel =stylesheethref =style.csstype =text / css/>
< / head>
< body>
<?php
require_once'functions.php';
getBar(); //带菜单/登录等的酒吧顶部
?>

我不认为显示functions.php会增加很多。我知道我不应该在我的文件中使用2 < head> s,那么如果我想在每个页面上都有描述,我该怎么办?我以前的文件设置如下:
index.php

 <?php 
session_start ();
include_once'header.php';
?>
//额外页面特定的东西,如描述或JS
< / head>
< body>
<?php
require_once'functions.php';
getBar(); //带菜单/登录等的酒吧顶部
?>
//页面内容
< / body>
< / html>

header.php

 <!DOCTYPE HTML> 
< html lan =en>
< head>
< meta charset =utf-8/>
< link rel =stylesheethref =style.csstype =text / css/>

然而,我认为这些文件是碎片化的。我不知道是否有这样做的普遍接受的方式,但如果有的话,我很乐意听到它!

解决方案

我认为大多数有经验的编码人员会告诉你尽可能少地混合你的代码和你的HTML。这意味着完成所有的计算,然后只是将视图所需的任何变量传递给视图。



他们还会告诉您查看MVC设计模式。在PHP中,这是控制器类获取初始请求的地方,在正确的业务模型类和放大器上执行正确的操作。然后将所需的数据传递给View类,它将呈现您的HTML。



MVC可以从非常简单的3类变体,到极其复杂的框架(比如Zend Framework)。如果您构建自己的,请查看PHP类自动加载,因为它会让您的生活更轻松。


I currently have a very simple page, with some more complicated backend. I've always done this the same way, but I feel it's not right. But I haven't come up with much useful to me.

I've got my index.php, header.php and function.php. index.php includes header.php, which calls a function from function.php. The main thing I'm not sure about is how to make the website dynamic enough that it can have easily editable pages, but also be easily editable is a big part of it needs editing.
index.php

<?php
session_start();
include_once 'header.php';
?>
// Page content
</body>
</html>

header.php

<!DOCTYPE HTML>
<html lan="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php
require_once 'functions.php';
getBar(); // Top of page bar with menu/login etc
?>

I don't think showing functions.php would add that much. I know I shouldn't use 2 <head>s in my file, so what do I do if I want to have a description on each page? I've previously had the files setup like: index.php

<?php
session_start();
include_once 'header.php';
?>
// extra page specific stuff here, such as description or JS
</head>
<body>
<?php
require_once 'functions.php';
getBar(); // Top of page bar with menu/login etc
?>
// Page content here
</body>
</html>

header.php

<!DOCTYPE HTML>
<html lan="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css" type="text/css" />

However, I think the files are "fragmented". I don't know if there is a generally accepted way of doing this, but if there is, I'd love to hear it!

解决方案

I think most experienced coders would tell you to mix your code and your HTML as little as possible. This means doing all of your calculations, then simply passing any variables needed by the view to the view.

They would also tell you to look into the MVC design pattern. In PHP, this is where Controller classes take the initial request, perform the correct operations on the correct business Model classes & then pass needed data to a View class, which would render your HTML.

MVC can go from very simple 3 class variations, to extremely complex "Frameworks" like Zend Framework. If you build your own, look into PHP class autoloading, as it will make your life easier.

这篇关于PHP中HTML头的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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