Codeigniter-如何扩展两个核心类


Codeigniter - How to extend two core classes

我也是Codeigniter和PHP的新手
我想问我能从
扩展两个类吗

system/core/some_file


进入

application/core/MY_some_file?


我试图为某个包含不允许字符的url创建一个自定义异常错误,所以如果有不允许的字符,应该重定向到我的自定义控制器。

这是我的自定义核心文件(my_URI):

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_URI extends CI_URI{
    function __construct(){
        parent::__construct();
    }
    function _filter_uri($str){
        if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE)
        {
            if ( ! preg_match("|^[".str_replace(array('''-', ''-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-'))."]+$|i", $str))
            {
                $this->load->view('page_not_found_v');
            }
        }
        // Convert programatic characters to entities
        $bad    = array('$',        '(',        ')',        '%28',      '%29');
        $good   = array('&#36;',    '&#40;',    '&#41;',    '&#40;',    '&#41;');
        return str_replace($bad, $good, $str);
    }
}

我试图加载该视图,但它无法加载。

这是系统工作流的早期阶段,因此您还无法访问某些对象
但您可以使用自定义错误页面:
在application/errors文件夹中创建一个名为error_400.PHP的PHP文件例如具有该内容。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Error</title>
</head>
<body>
<div id="container">
    <h1><?php echo $heading; ?></h1>
    <?php echo $message; ?>
</div>
</body>
</html>

(但也许您可以复制error_general.php并根据需要进行修改)
然后在重写的URI类中,您可以这样显示自定义页面(而不是重定向):

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_URI extends CI_URI {
    /**
     * Filter segments for malicious characters
     *
     * @access  private
     * @param   string
     * @return  string
     */
    function _filter_uri($str)
    {
        if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE)
        {
            // preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
            // compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
            if ( ! preg_match("|^[".str_replace(array('''-', ''-'), '-', preg_quote($this->config->item('permitted_uri_chars'), '-'))."]+$|i", $str))
            {
                $_error =& load_class('Exceptions', 'core');
                echo $_error->show_error('The URI you submitted has disallowed characters.', 'The URI you submitted has disallowed characters.', 'error_400', 400);
                exit;
            }
        }
        // Convert programatic characters to entities
        $bad    = array('$',        '(',        ')',        '%28',      '%29');
        $good   = array('&#36;',    '&#40;',    '&#41;',    '&#40;',    '&#41;');
        return str_replace($bad, $good, $str);
    }
}