我在网站上工作 - 只是学习改进我的编码。 我有一个像这样工作的路由系统:
/config/routes.php:
$route->add('/' , function() {
require_once("/application/pages/index.php");
});
$route->add('/register', function() {
require_once("/application/pages/register.php");
});
$route->add('/login', function() {
require_once("/application/pages/login.php");
});
$route->add('/logout', function() {
require_once("/application/pages/logout.php");
});
$route->add('/panel', function() {
require_once('/application/pages/panel/index.php');
});
在我的index.php中:
require_once('application/pages/header.php');
include('config/routes.php');
require_once('application/pages/footer.php');
一切正常但我需要一个不同的header.php和footer.php用于用户进入面板时。文件:/application/pages/panel/index.php
当我在panel / index.php中require_once一个新的头文件时,会加载新旧头文件。如何取消/panel/index.php中的页眉和页脚文件,以便我可以要求不同的文件?有什么建议吗?
答案 0 :(得分:2)
注意:路由来自MVC design pattern,您应该将控制器与视图分开。
模板和视图也可以保持独立。这意味着我们的目录设置可能如下所示:
- Templates
- header_one.php
- footer_one.php
- header_two.php
- footer_two.php
- Views
- index.php
- someOtherBody.php
这是一个简单但未完成(这是你的挑战)的一个对象示例,可以做我正在解释的内容:
class Page {
private $template_path = dirname(dirname(__FILE__)) . '/templates/';
private $view_path = dirname(dirname(__FILE__)) . '/views/';
protected $header;
protected $footer;
protected $body;
public function setHeader($file_name)
{
if(is_readable($this->template_path . $file_name))
{
$this->header = $this->template_path . $file_name;
return $this;
}
// add an exception
}
/* TODO: */
public function setFooter() {}
public function setBody() {}
/* Render page */
public function render()
{
$page = [$this->header,$this->body,$this->footer];
foreach($page as $file)
{
require_once($file);
}
}
}
这里的想法是我们可以在路由方法闭包中使用上面的对象设置我们的页面布局,然后在逻辑之后渲染/需要所有文件。
$route->add('/', function() {
$page = new Page();
$page->setHeader('header_one.php')
->setBody('index.php')
->setFooter('footer_one.php');
/* todo: add your logic here */
$page->render();
});
每条路线现在都有自己的页眉,页脚和正文 希望这有帮助。
答案 1 :(得分:1)
在你的地方,我会做那样的事情:
使用输出缓冲区并检查是否已经需要该文件。我给你一个简单的例子,但为你调整代码。 并检查功能:http://php.net/manual/en/function.get-included-files.php
$route->add('/panel', function() {
include_once('YOUR_SPECIFIC_PATH/header.php');
require_once('/application/pages/panel/index.php');
include_once('YOUR_SPECIFIC_PATH_header/footer.php');
});
并且:
ob_start();
include_once('config/routes.php');
$mainContent = ob_get_contents();
ob_end_clean();
include_once('application/pages/header.php');
echo $mainContent;
include_once('application/pages/footer.php');
我没有时间寻求帮助,但如果您需要,我可以稍后解释
答案 2 :(得分:0)
此解决方案要求您在子控制器(header.php
)所在的每个文件夹中都有footer.php
和application/<module name>/index.php
。
index.php 仅通过路由调用您的子控制器:
// require not include, because "no routing" = "no web site" ;)
require_once('config/routes.php');
application / pages / index.php 包含适当的页眉/页脚w /相对路径
require_once('header.php');
// page code
require_once('footer.php');
application / register / index.php 包含适当的页眉/页脚w /相对路径
require_once('header.php');
// page code
require_once('footer.php');
<强>等强>
答案 3 :(得分:0)
@KDOT,感谢您的帮助,但是使用您的代码我收到了一个我无法解决的错误:
$route->add('/', function() {
$page = new Page('header.php', 'home.php', 'footer.php');
$page->render();
});
但是由于你的代码,我设法以我的方式重写了这个类,现在它可以工作了;) 再次感谢@KDOT!
如果有人需要它:
:uch
cls
echo.
echo Are you sure you want to change your username?
echo.
echo [Y/N]
echo.
set /p input=
if %input% EQU n goto set
if %input% NEQ y goto uch
:ucy
cls
echo.
echo Enter your current username
echo.
set /p cuser=
if %cuser% NEQ %username1% (
echo.
echo Incorrect username. Please try again.
echo Press any button to continue.
echo.
pause>null
goto :ucy
)
setlocal enableDelayedExpansion
if %cuser% EQU %username1% (
echo Please enter new username.
echo.
set /p nuser=
echo Please enter again.
echo.
set /p nuser2=
if !nuser2! EQU !nuser! set username1=!nuser!
if !nuser2! EQU !nuser! goto ga1
if !nuser2! NEQ !nuser! (
echo Usernames do not match. Please try again.
echo Press any button to continue.
echo.
pause>null
goto ucy
)
goto ucy
和
{{1}}