2016-03-24 166 views
0

我是Codeigniter和PHP的noob。
我想问一下,我可以从
Codeigniter - 如何扩展两个核心类

系统/核心扩展两个类/ SOME_FILE



应用/核心/ 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); 
    } 
} 

我试图加载看法,但它不能加载它。

回答

0

这是系统工作流程的一个早期点,因此您还无法访问某些对象。
但是,您可以使用自定义错误页面:
在应用程序/错误文件夹中创建一个PHP文件,名称为:error_400.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); 
    } 
} 

+0

对于现在的错误不能访问MY_URI文件时,它总是从加载消息系统/核心中的URI.php –

+0

现在并不是说MY_URI没有加载,但是错误信息并不像我在MY_URI中编辑的那样出现,并且应该编辑的错误页面位于文件夹** view/error/HTML ** –