|
Development -
CodeIgniter
|
|
Автор: Sorbing
10.03.11 19:08
|
|
Что позволяет базовая схема CodeIgniter-а при обработке, а точнее при отображении ошибок пользователю?
Давайте посмотрим:
- Вывести сообщение об ошибке, используя заданый шаблон application/errors/error_general.php: show_error('message');
- Отобразить сообщение что файл не найден (404-я ошибка) в шаблоне application/errors/error_404.php: show_404('page');
- Записать сообщение в лог файл в папке logs: log_message('level', 'message'); ('level' - уровень критичности debug, error, info). Этот вариант явно не для пользователя ).
Как видим, есть два доступных обработчика ошибок. А что если нам нужно проверять авторизацию, уровень привилегий, регион, и т.п, и в зависимости от этого выполнить определенные действия по обработке и выводу 404-й ошибки.
Вот решение для расширенной обработки 404-й ошибки:
- Создаем контроллер для обработки 404-й ошибки:
//system/application/controllers/error.php
class Error extends CI_Controller
{
function error_404()
{
$this->output->set_status_header('404');
echo "404 - not found";
}
}
- Заменим библиотеку CI_Router:
class MY_Router extends CI_Router {
var $error_controller = 'error';
var $error_method_404 = 'error_404';
function __construct()
{
parent::__construct();
}
// this is just the same method as in Router.php, with show_404() replaced by $this->error_404();
function _validate_request($segments)
{
// Does the requested controller exist in the root folder?
if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
{
return $segments;
}
// Is the controller in a sub-folder?
if (is_dir(APPPATH.'controllers/'.$segments[0]))
{
// Set the directory and remove it from the segment array
$this->set_directory($segments[0]);
$segments = array_slice($segments, 1);
if (count($segments) > 0)
{
// Does the requested controller exist in the sub-folder?
if ( ! file_exists(APPPATH . 'controllers/' . $this->fetch_directory() . $segments[0] . EXT))
{
return $this->error_404();
}
}
else
{
$this->set_class($this->default_controller);
$this->set_method('index');
// Does the default controller exist in the sub-folder?
$fname = APPPATH . 'controllers/' . $this->fetch_directory() . $this->default_controller . EXT;
if ( ! file_exists($fname))
{
$this->directory = '';
return array();
}
}
return $segments;
}
// Can't find the requested controller...
return $this->error_404();
}
function error_404()
{
$this->directory = "";
$segments = array();
$segments[] = $this->error_controller;
$segments[] = $this->error_method_404;
return $segments;
}
function fetch_class()
{
// if method doesn't exist in class, change
// class to error and method to error_404
$this->check_method();
return $this->class;
}
function check_method()
{
$ignore_remap = true;
$class = $this->class;
if (class_exists($class))
{
// methods for this class
$class_methods = array_map('strtolower', get_class_methods($class));
// ignore controllers using _remap()
if($ignore_remap && in_array('_remap', $class_methods))
{
return;
}
if (! in_array(strtolower($this->method), $class_methods))
{
$this->directory = "";
$this->class = $this->error_controller;
$this->method = $this->error_method_404;
include(APPPATH . 'controllers/' . $this->fetch_directory() . $this->error_controller . EXT);
}
}
}
function show_404()
{
include(APPPATH . 'controllers/' . $this->fetch_directory() . $this->error_controller . EXT);
call_user_func(array($this->error_controller, $this->error_method_404));
}
}
/* End of file MY_Router.php */
/* Location: ./system/application/libraries/MY_Router.php */
- Чтобы сгенерировать ошибку 404 из контроллера нужно вызвать метод show_404() класса Router():
$this->router->show_404();
- Пользуемся ).
Источник: maestric.com
|
|
Последнее обновление: 09.04.11 18:13 |