简体   繁体   English

PHP 中的动态链接

[英]Dynamic linking in PHP

I am creating a custom CMS for learning.我正在创建一个用于学习的自定义 CMS。 I have the plan to have the following pages,我计划有以下几页,

  1. Login page登录页面
  2. All Posts page所有帖子页面
  3. Edit Post page编辑帖子页面
  4. index page索引页
  5. header.php (the website's header) header.php(网站的标题)
  6. footer.php (the website's footer) footer.php(网站的页脚)
  7. sidebar.php (the website's sidebar) sidebar.php(网站的侧边栏)

I am confused how would the index page link header, footer and sidebar.我很困惑索引页如何链接页眉、页脚和侧边栏。 Please guide me how can I link these php files in index.php.请指导我如何在 index.php 中链接这些 php 文件。

You can simply add an Array of files you want to include:您可以简单地添加要包含的文件数组:

$array = ('header.php', 'footer.php', 'sidebar.php');

Then add some HTML Code structure...然后添加一些 HTML 代码结构...

and then you can access the Array and load files.然后您可以访问数组并加载文件。

include_once($array[0]);

.. to include the header.php .. 包含 header.php

include_once($array[1]);

.. to include the footer.php .. 包含footer.php

.... ....

you can use the require_once function to make your site not loading other content if the file does not exists.如果文件不存在,您可以使用 require_once 函数使您的站点不加载其他内容。

if you want to add these files automaticly just add a loop.如果你想自动添加这些文件,只需添加一个循环。

foreach($array as $file){
if(file_exists($file)){
require_once($file);
}
else{
die($file.' does not exist!');
}
}

You can use either require_once or include .您可以使用require_onceinclude I personally use require once option cause it only needs to be include once and not again on later stage.我个人使用 require once 选项,因为它只需要包含一次,而不会在后期再次包含。

Inside your body tags:在你的body标签:

require_once('/path/to/header.php');
require_once('/path/to/breadcrumb.php');
require_once('/path/to/content.php');
require_once('/path/to/footer.php');
require_once('/path/to/copyright.php');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM