Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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 Kohana-关闭时显示缓冲视图或自定义视图_Php_Model View Controller_Kohana - Fatal编程技术网

Php Kohana-关闭时显示缓冲视图或自定义视图

Php Kohana-关闭时显示缓冲视图或自定义视图,php,model-view-controller,kohana,Php,Model View Controller,Kohana,我用导数来表达我的观点。我想在关机时显示缓冲视图或自定义视图,例如 如果在运行时出现一些错误/异常=>显示错误页面 如果执行时没有错误=>显示缓冲输出 视图 class View { private $file = ''; private $data = array(); private static $global_data = array(); public static $view = ''; private function __construct($file = FALSE,

我用导数来表达我的观点。我想在关机时显示缓冲视图或自定义视图,例如

  • 如果在运行时出现一些错误/异常=>显示错误页面
  • 如果执行时没有错误=>显示缓冲输出
  • 视图

    class View
    {
    
    private $file = '';
    
    private $data = array();
    
    private static $global_data = array();
    
    public static $view = '';
    
    private function __construct($file = FALSE, array $data = array())
    {
        if ($file !== FALSE)
        {
            $this->set_filename($file);
        }
    
        if (!empty($data))
        {
            $this->data = $data + $this->data;
        }
    }
    
    /**
     * Creates new view object and returns it.
     *
     * @param string filename
     * @param array variables
     * @return object View
    */
    public static function factory($file = FALSE, array $data = array())
    {
        return new View($file, $data);
    }
    
    /**
     * Captures the output that is generated when a view is included.
     * The view data will be extracted to make local variables. This method
     * is static to prevent object scope resolution.
     *
     * @param string filename
     * @param array variables
     * @return string
    */
    public static function capture($view_filename, array $view_data)
    {   
        extract($view_data, EXTR_SKIP);
    
        ob_start();
    
        try
        {
            require $view_filename;
        }
        catch (Exception $error)
        {
            $ob_handlers = ob_list_handlers();
    
            if (!empty($ob_handlers))
            { 
                ob_end_clean();
            }
    
            throw $error;
        }
    
        return ob_get_clean();
    }
    
    /**
    * Load view file
    *
    * @param string filename
    * @return boolean
    * @return object View
    */
    public function set_filename($file)
    {
        if (strpos($file, APP_DIR) === FALSE)
        {
            $extension = strrpos($file, '.') === FALSE ? '.php' : '';
    
            $path = APP_DIR.DIR_SEP.'system'.DIR_SEP.'views'.DIR_SEP.$file.$extension;
        }
        else
        {
            $path = $file;
        }
    
        if (!file_exists($path))
        {
            Error::throw_throwable('Unable to find file '.$path);
        }
    
        $this->file = $path;
    
        return $this;
    }
    
    /**
    * Sets a global variable, similar to the set() method.
    *
    * @param string variable name
    * @param mixed variable value
    * @return object View
    */
    public static function set_global($key, $value = FALSE)
    {
        self::$global_data[$key] = $value;
    }
    
    /**
    * Assigns a variable by name. 
    *
    * @param string variable name or an array of variables
    * @param mixed variable value
    * @return object View
    */
    public function set($key, $value = FALSE)
    {
        if (is_array($key))
        {
            foreach ($key as $name => $value)
            {
                $this->data[$name] = $value;
            }
        }
        else
        {
            $this->data[$key] = $value;
        }
    
        return $this;
    }
    
    /**
    * Renders the view object to a string. 
    *
    * @throws exception
    * @param string filename
    * @return string
    */
    public function render($file = FALSE)
    {
        if ($file !== FALSE)
        {
            $this->set_filename($file);
        }
    
        if (empty($this->file))
        {
            Error::throw_throwable('Unable to find file '.$this->file);
        }
    
        $data = array_merge(View::$global_data, $this->data);
    
        return View::capture($this->file, $data);
    }
    
    public function __toString()
    {
        try
        {
            $result = $this->render();
    
            return $result;
        }
        catch (Exception $error)
        {
            Error::throw_throwable($error);
        }
    }
    
    public function __set($key, $value)
    {
        $this->set($key, $value);
    }
    
    public function __get($key)
    {
        return isset($this->data[$key]) ? $this->data[$key] : FALSE;
    }
    
    }
    
    用法


    前面几行所做的是回显模板视图内部的内容视图。这也应该是我的关机处理程序在必要时响应的结果。有没有一种好的/简单的方法可以做到这一点?

    听起来您想要的是在出现错误时显示自定义错误页面。这里我通常要做的是扩展
    Kohana\u Kohana\u异常
    类(作为
    Kohana\u异常
    )并重写
    公共静态函数处理程序
    方法

    这使您可以输入一些代码来检查错误是什么(HTTP_404; u异常或其他),环境是什么(开发/生产),并执行您想要的任何行为

    在我的例子中,类似这样的情况:

    // If not production and not an HTTP exception
    if ( ! ($e instanceof HTTP_Exception) && Kohana::$environment !== Kohana::PRODUCTION)
    {
        // Use built in error handler to show stace trace for developers
        Kohana_Kohana_Exception::handler($e);
        // ... exit(1);
    }
    
    // Otherwise
    $injected_routes = array(Route::get('error'));
    echo Request::factory('error-uri', NULL, FALSE, $injected_routes)
                    ->headers(Request::$initial->headers())
                    ->execute()
                    ->send_headers(TRUE)
                    ->body();
    
    在这里,您需要另一个名为
    error
    的路由,该路由与
    error uri
    匹配,并转到另一个错误控制器/操作/视图,您可以使用它来呈现自定义错误页面



    要使其正常工作,您需要通过将
    'errors'=>TRUE
    传递给
    Kohana::init
    调用
    bootstrap.php

    来启用错误
    View\u Class
    背后的概念似乎很完美-我没有改变它。但是,我为此编写了一个包装器—另一个静态
    View\u Class
    方法,用于检查错误数组是否为空。但是,如果您将Kohana框架作为一个整体使用,那么这也值得一看。作品很有魅力,将尝试复制/重写到我自己的课堂上。:)
    // If not production and not an HTTP exception
    if ( ! ($e instanceof HTTP_Exception) && Kohana::$environment !== Kohana::PRODUCTION)
    {
        // Use built in error handler to show stace trace for developers
        Kohana_Kohana_Exception::handler($e);
        // ... exit(1);
    }
    
    // Otherwise
    $injected_routes = array(Route::get('error'));
    echo Request::factory('error-uri', NULL, FALSE, $injected_routes)
                    ->headers(Request::$initial->headers())
                    ->execute()
                    ->send_headers(TRUE)
                    ->body();