Tạo trang 404 trong Codeigniter

Tạo trang 404 trong Codeigniter

Phan Nhật Chánh

Chánh14 tháng 04, 2020

3 min read
·
views
·
likes

Trang 404 là một trang thông báo lỗi trên web được sử dụng để hiển thị cho người dùng khi truy cập một URL không tồn tại hoặc không thể tìm thấy trên máy chủ web. Mã lỗi 404 được trả về khi máy chủ web không thể tìm thấy tài nguyên yêu cầu được yêu cầu bởi trình duyệt của người dùng. Trang 404 thường chứa thông báo lỗi và gợi ý cho người dùng để điều hướng đến các trang web khác hoặc liên hệ với quản trị viên trang web để biết thêm thông tin. Trang 404 là một phần quan trọng của các trang web và cần được thiết kế một cách đáp ứng và thân thiện để cung cấp trải nghiệm tốt cho người dùng.

Trang báo lỗi 404 sẽ xuất hiện khi người dùng đi tới các liên kết bị hỏng, đã bị xóa. Vậy làm thế nào để tạo trang error 404 trong Codeigniter mời bạn làm theo các bước dưới đây.

Routes

  • Mở tệp tin routes.php tại đường dẫn application/config/route.php
  • Sửa thành $route['404_override'] = 'Custom404';

Ở đây, Custom404 là tên của tệp trong controller.

Controller

  • Tạo một tệp Custom404.php trong đường dẫn application/controllers/ và thêm đoạn mã dưới đây:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Custom404 extends CI_Controller { public function __construct() { parent::__construct(); // load base_url $this->load->helper('url'); } public function index(){ $this->output->set_status_header('404'); $this->load->view('error404'); } }
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Custom404 extends CI_Controller { public function __construct() { parent::__construct(); // load base_url $this->load->helper('url'); } public function index(){ $this->output->set_status_header('404'); $this->load->view('error404'); } }

View

Tạo một tệp error404.php trong đường dẫn application/views/ và thêm nội dung thông báo lỗi 404. Ví dụ:

<!DOCTYPE html> <html> <head> <title>404 Page Not Found</title> <style> body { width: 99%; height: 100%; background-color: mediumturquoise; color: white; font-family: sans-serif; } div { position: absolute; width: 400px; height: 300px; z-index: 15; top: 45%; left: 50%; margin: -100px 0 0 -200px; text-align: center; } h1, h2 { text-align: center; } h1 { font-size: 60px; margin-bottom: 10px; border-bottom: 1px solid white; padding-bottom: 10px; } h2 { margin-bottom: 40px; } a { margin-top: 10px; text-decoration: none; padding: 10px 25px; background-color: ghostwhite; color: black; margin-top: 20px; } </style> </head> <body> <div> <h1>404</h1> <h2>Page not found</h2> <a href="<?= base_url(); ?>">Back to Homepage</a> </div> </body> </html>
<!DOCTYPE html> <html> <head> <title>404 Page Not Found</title> <style> body { width: 99%; height: 100%; background-color: mediumturquoise; color: white; font-family: sans-serif; } div { position: absolute; width: 400px; height: 300px; z-index: 15; top: 45%; left: 50%; margin: -100px 0 0 -200px; text-align: center; } h1, h2 { text-align: center; } h1 { font-size: 60px; margin-bottom: 10px; border-bottom: 1px solid white; padding-bottom: 10px; } h2 { margin-bottom: 40px; } a { margin-top: 10px; text-decoration: none; padding: 10px 25px; background-color: ghostwhite; color: black; margin-top: 20px; } </style> </head> <body> <div> <h1>404</h1> <h2>Page not found</h2> <a href="<?= base_url(); ?>">Back to Homepage</a> </div> </body> </html>

Kết quả

Hướng dẫn tạo trang báo lỗi 404 trong codeigniter framework

Khi bạn truy cập vào một địa chỉ không tồn tại, không có sẵn thì hệ thống sẽ hiển thị trang báo lỗi tương tự như hình trên. Chúc thành công!