2016-03-24 85 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 ** –