【问题标题】:unrequire a php file不需要一个php文件
【发布时间】:2017-04-25 01:50:19
【问题描述】:

我在网站上工作 - 只是在学习改进我的编码。 我有一个像这样工作的路由系统:

/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 中需要一个新的头文件时,新旧头文件都会被加载。如何取消 /panel/index.php 中的页眉和页脚文件,以便需要不同的文件?有什么建议吗?

【问题讨论】:

  • 请编辑您的帖子并添加您收到的确切消息
  • @WEBjuju - 抱歉,我的意思是两个头文件都在加载。我不知道为什么我写了有和错误,可能是因为我现在很累。

标签: php routing routes require


【解决方案1】:

注意:路由来自MVC design pattern,您应该将控制器与视图分开。


模板视图也可以分开保存。这意味着我们的目录设置可能如下所示:

- Templates
    - header_one.php
    - footer_one.php
    - header_two.php
    - footer_two.php
- Views
    - index.php
    - someOtherBody.php

这是一个简单但未完成(这是您的挑战)的 Object 示例,它可以执行我所解释的操作:

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();
});

现在每条路由都可以有自己的页眉、页脚和正文。
希望这会有所帮助。

【讨论】:

  • 我插入了这个类并完成了它,但我得到一个错误:Parse error: syntax error, unexpected '(', expecting ',' or ';' in C:\wampserver\www\cms\application\classes\class_page.php on line 17 第 17 行:const TEMPLATE_PATH = dirname(dirname(__FILE__)) . '/application/templates/';
  • 此代码 sn-p 不是工作版本。生成该概念的工作版本是您自己的责任。首先,您需要更改目录布局,这意味着将所有视图移动到 views 目录 并与模板相同。注意:确保return $this; 保持方法之间的 OO 流。
  • 您可以通过该链接查看更多关于dirname()的信息。
  • 不过,我已经更新了解决该问题的答案。常量必须是字符串/整数。它们不能是方法。我的错。虽然您可以define('TEMPLATE_PATH' , dirname(__FILE_) . '/template'); 然后将其称为TEMPLATE_PATH
【解决方案2】:

在你的地方,我会做这样的事情:

使用缓冲区并检查文件是否已被需要。我给你一个简单的例子,但为你调整代码。 并检查功能: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');

抱歉,我没有时间寻求帮助,但如果您需要,我可以稍后解释

【讨论】:

  • 我越来越同意 php 社区的观点,他们认为ob_ 函数通常表明存在设计缺陷。这当然是其中一种情况。一方面,它增加了必要的代码行,而这些代码行显然可以减少。
【解决方案3】:

此解决方案要求您在子控制器 (application/<module name>/index.php) 所在的每个文件夹中都有 header.phpfooter.php

index.php 仅通过路由调用您的子控制器:

// require not include, because "no routing" = "no web site" ;)
require_once('config/routes.php');

application/pages/index.php 包含适当的页眉/页脚和相对路径

require_once('header.php');
// page code
require_once('footer.php');

application/register/index.php 包含适当的页眉/页脚和相对路径

require_once('header.php');
// page code
require_once('footer.php');

【讨论】:

  • 如果您的 application/pages/header.php 是“默认”页眉(和页脚),那么在您想要调用它的任何文件夹/模块中,而不是文件夹存在的 header.php 中,相对路径将可能是../pages/header.php 所以然后是require_once('../pages/header.php');
【解决方案4】:

@KDOT,感谢您的帮助,但使用您的代码我遇到了无法修复的错误:

 Call to a member function setBody() on null

但是感谢您的代码,我设法以自己的方式重写了该类,现在它可以工作了;) 再次感谢@KDOT!

如果有人需要:

class Page {
    private $TEMPLATE_PATH = '/application/templates/';
    private $VIEW_PATH = '/application/views/';

    protected $header;
    protected $footer;
    protected $body;

    public function __construct($header_file, $body_file, $footer_file) {
        $this->header = $this->TEMPLATE_PATH . $header_file;
        $this->body = $this->VIEW_PATH . $body_file;
        $this->footer = $this->TEMPLATE_PATH . $footer_file;
    }

    public function render(){
        $page = [$this->header, $this->body, $this->footer];
        foreach($page as $file) {
            require_once($file);
        }
    }
}

和:

$route->add('/', function() {
    $page = new Page('header.php', 'home.php', 'footer.php');
    $page->render();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    相关资源
    最近更新 更多