Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不需要一个php文件_Php_Routing_Routes_Require - Fatal编程技术网

不需要一个php文件

不需要一个php文件,php,routing,routes,require,Php,Routing,Routes,Require,我在一个网站上工作——只是学习改进我的编码。 我有一个路由系统,其工作原理如下: - Templates - header_one.php - footer_one.php - header_two.php - footer_two.php - Views - index.php - someOtherBody.php /config/routes.php: $route->add('/' , function() { requir

我在一个网站上工作——只是学习改进我的编码。 我有一个路由系统,其工作原理如下:

- Templates
    - header_one.php
    - footer_one.php
    - header_two.php
    - footer_two.php
- Views
    - index.php
    - someOtherBody.php
/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'); 
});
在my 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中的页眉和页脚文件,以便需要不同的文件?有什么建议吗?

此解决方案要求在子控制器(
应用程序//index.php
)所在的每个文件夹中都有一个
header.php
footer.php

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

// require not include, because "no routing" = "no web site" ;)
require_once('config/routes.php');
应用程序/pages/index.php包括适当的页眉/页脚和相对路径

require_once('header.php');
// page code
require_once('footer.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');
require_once('header.php');
// page code
require_once('footer.php');

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

使用out buffer并检查文件是否已被需要。我给你一个简单的例子,但是为你修改代码。 并检查功能:

以及:


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

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


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

- 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方法闭包中设置页面布局,然后在逻辑之后呈现/需要所有文件



现在,每条路线都可以有自己的页眉、页脚和正文。

希望这有帮助。

@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);
        }
    }
}
以及:


请编辑您的帖子并添加您想要的确切信息receiving@WEBjuju-对不起,我错了,我的意思是两个头文件都在加载。我不知道为什么我写了这样的代码和错误,可能是因为我现在累了。如果你的
应用程序/pages/header.php
是“默认”的页眉(和页脚),那么在你想调用它的任何文件夹/模块中,而不是现有的文件夹
header.php
,相对路径可能是
。/pages/header.php
,因此
需要一次('../pages/header.php')我越来越同意php社区的观点,他们认为
ob\uu
函数通常表明存在设计缺陷。这当然是其中之一。首先,它增加了必要的代码行,而这些代码行可以明显减少。我插入了这个类并完成了它,但我得到了一个错误:
Parse error:syntax error,unexpected'第17行的C:\wampserver\www\cms\application\classes\class\u page.php中的“,”应为“,”或“;”,
17行:
const TEMPLATE\u PATH=dirname(dirname(\u FILE\uu))“/application/templates/”;
此代码段不是一个工作版本。生成此概念的工作版本是您自己的责任。首先,您需要更改目录布局,这意味着将所有视图移动到视图目录中,并与模板相同。注意:确保
返回$This;
以保持O方法之间的流动。您可以根据该链接查看有关的更多信息。但是,我已更新了解决该问题的答案。常量必须是字符串/整数。它们不能是方法。我的错误。尽管您可以
定义('TEMPLATE_PATH',dirname('u文件.');
然后将其称为
TEMPLATE_PATH
$route->add('/', function() {
    $page = new Page('header.php', 'home.php', 'footer.php');
    $page->render();
});