Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.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
MVC博客-如何包含页眉\footer\other.php文件_Php_Html_Sql_Model View Controller - Fatal编程技术网

MVC博客-如何包含页眉\footer\other.php文件

MVC博客-如何包含页眉\footer\other.php文件,php,html,sql,model-view-controller,Php,Html,Sql,Model View Controller,在本教程之后,我将尝试编写我的第一个MVC博客 我了解它的工作原理,router.php调用合适的页面控制器。该控制器调用模型,然后调用带有返回值的视图页面 我的问题是,如果我想在同一news.php页面中添加页眉\页脚,该怎么办。通常我会写“include”(“header.php”)。但现在使用MVC时,我该如何实现它呢 这些是我的文件: router.php: 在MVC结构中没有很好的解决方案。你可以在每个框架中找到解决方案。例如,在CakePHP中,你可以使用AppControlle

在本教程之后,我将尝试编写我的第一个MVC博客

我了解它的工作原理,router.php调用合适的页面控制器。该控制器调用模型,然后调用带有返回值的视图页面

我的问题是,如果我想在同一news.php页面中添加页眉\页脚,该怎么办。通常我会写“include”(“header.php”)。但现在使用MVC时,我该如何实现它呢

这些是我的文件:

router.php:



在MVC结构中没有很好的解决方案。你可以在每个框架中找到解决方案。例如,在CakePHP中,你可以使用AppController,一个总是为每个请求调用的控制器。类似于基本控制器

但是不,做得不太好

最简单的方法是直接从视图中查询数据。这听起来很奇怪,但在MVC结构中是可以接受的。因此,在您的视图中,您可以调用$News->getLatestItems(10);并使用它们

我不喜欢,但它很管用

如果你有很多小部件和块,单是MVC可能不是正确的结构,那么你可以看看:这是MVC的派生


另一个越来越多的解决方案是:通过AJAX调用进行请求。因此,只需在页面加载后加载它们。这样,您也可以解决问题,因为一个MVC请求会变成多个MVC请求。

部分问题在于,您的教程对MVC启发的设计模式有相当原始的解释(实际上,您无法在PHP中实现经典的MVC,但有一些是基于它的)

视图不仅仅是一个模板。视图应该是类实例,其中包含所有表示逻辑并处理多个模板。实际上,您拥有的是一个
布局
模板,其中包含
帖子
模板

// class \Application\View\Posts
public function render()
{
    $layout = new Template( $this->defaultTemplateDirectory . 'layout.html');
    $content = new Template( $this->defaultTemplateDirectory . 'posts.html' );

    $layout->assign( 'content' , $content->render() );

    return $layout->render();
}
此外,视图实例应该做的事情之一是从模型层请求信息

您可能会发现一些有用的材料:

如果你想扩展OOP方面的知识,这里有一个推荐讲座和书籍的列表

<?php
    /**
     * This file handles the retrieval and serving of posts posts
     */
    class Posts_Controller
    {
        /**
         * This template variable will hold the 'view' portion of our MVC for this 
         * controller
         */
        public $template = 'posts';

        /**
         * This is the default function that will be called by router.php
         * 
         * @param array $getVars the GET variables posted to index.php
         */
        public function main(array $getVars)
        {
            //$b_controller =new  Bottom_Bar_Controller;
            //$b_controller->main($getVars);

            $postsModel = new Posts_Model;

            //get an post
            $post = $postsModel->get_post($getVars['id']);
            //create a new view and pass it our template
            $header = new View_Model('header_template');
            $bottom_bar = new View_Model('bottom_bar');
            $view = new View_Model($this->template);


            //assign post data to view
            $view->assign('header', $header->render(FALSE));
            $view->assign('bottom', $bottom_bar->render(FALSE));
            $view->assign('title' , $post['title']);
            $view->assign('content' , $post['content']);
            $view->assign('date' , $post['date']);
            $view->assign('by' , $post['added_by']);
            $view->render();

        }
    }
<?php
/**
 * The Posts Model does the back-end heavy lifting for the Posts Controller
 */
class Posts_Model
{
    /**
     * Holds instance of database connection
     */
    private $db;

    public function __construct()
    {
        $this->db = new MysqlImproved_Driver;
    }

    /**
     * Fetches article based on supplied name
     * 
     * @param string $author
     * 
     * @return array $article
     */
    public function get_post($id)
    {        
        //connect to database
        $this->db->connect();

        //sanitize data
        $author = $this->db->escape($id);

        //prepare query
        $this->db->prepare
        (
            "
            SELECT *  FROM `posts`
            WHERE
                `id` = '$id'
            LIMIT 1
            ;
            "
        );

        //execute query
        $this->db->query();

        $article = $this->db->fetch('array');

        return $article;
    }

}
?>
<?php
/**
 * Handles the view functionality of our MVC framework
 */
class View_Model
{
    /**
     * Holds variables assigned to template
     */
    private $data = array();

    /**
     * Holds render status of view.
     */
    private $render = FALSE;

    /**
     * Accept a template to load
     */


    public function __construct($template)
    {

        //compose file name
        $file = SERVER_ROOT . '/views/' . strtolower($template) . '.php';


        if (file_exists($file))
        {

            /**
             * trigger render to include file when this model is destroyed
             * if we render it now, we wouldn't be able to assign variables
             * to the view!
             */
            $this->render = $file;


        }        
    }

    /**
     * Receives assignments from controller and stores in local data array
     * 
     * @param $variable
     * @param $value
     */
    public function assign($variable , $value)
    {
        $this->data[$variable] = $value;
    }

    /**
     * Render the output directly to the page, or optionally, return the
     * generated output to caller.
     * 
     * @param $direct_output Set to any non-TRUE value to have the 
     * output returned rather than displayed directly.
     */
    public function render($direct_output = TRUE)
    {

        // Turn output buffering on, capturing all output
        if ($direct_output !== TRUE)
        {
            ob_start();
        }

        // Parse data variables into local variables
        $data = $this->data;

        // Get template
        include($this->render);

        // Get the contents of the buffer and return it
        if ($direct_output !== TRUE)
        {
            return ob_get_clean();
        }
    }

    public function __destruct()
    {
    }
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="../style/style.css" rel="stylesheet" type="text/css" media="screen" />  
<title> Posts (View)</title>
</head>

<body>
       <div id="main">
        <div class="container">
        <?=$data['header'];?>
            <div id="content">  
                <div class="content-background">
                <h2> <?=$data['title'];?></h2>
                <h4> <?=$data['date'];?> </h4>
                <p><?=$data['content'];?></p>
                </div>

            </div>
         </div>
        </div>


    </body>
</html>
// class \Application\View\Posts
public function render()
{
    $layout = new Template( $this->defaultTemplateDirectory . 'layout.html');
    $content = new Template( $this->defaultTemplateDirectory . 'posts.html' );

    $layout->assign( 'content' , $content->render() );

    return $layout->render();
}