Suatu masalah memang terkadang menimbulkan kesulitan, tetapi bila kita bijak menghadapi dan dengan seksama meneliti sebelum memutuskan sebuah keputusan akan menimbulkan hasil yang berbeda, bukankah seperti itu pembaca? yup… Setidaknya menurut penulis seperti itu.
Awalnya penulis ingin membuat custom error pada framework codeigniter untuk header dengan status 401 atau Not Authorized, status ini biasanya dimunculkan apabila seorang user tidak berhak untuk mengakses suatu laman tertentu, misalnya karena belum login.
Setelah puas bergoogling akhirnya saya menemukan sebuah jawaban di forum untuk membuat custom error pada codeigniter, namun perlu dibuat modifikasi sedikit supaya bisa berjalan dengan baik mengingat postingan di forum tersebut untuk codeigniter versi lama.
Berikut hasil yang saya dapatkan, buatlah sebuah file di applications/core/My_Exceptions.php isinya sebagai berikut:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Exceptions extends CI_Exceptions { function __construct(){ parent::__construct(); } /** * 401 Not Authorized * * Called by * */ function show_401() { $heading = "401 Not Authorized"; $message = "You are not authorized to view this page."; echo $this->show_error($heading, $message, 'error_401',401); exit; } } |
Buatlah sebuah helper di applications/helpers/error_helper.php dan isinya sebagai beriktu :
1 2 3 4 5 6 7 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); function show_401(){ $exceptions =& load_class('Exceptions','core'); $exceptions->show_401(); } ?> |
Kemudian kita juga harus membuat halaman error-nya di applications/errors/error_401.php dan isikan sebagai berikut:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <!DOCTYPE html> <html lang="en"> <head> <title>401 Not Authorized</title> <style type="text/css"> ::selection{ background-color: #E13300; color: white; } ::moz-selection{ background-color: #E13300; color: white; } ::webkit-selection{ background-color: #E13300; color: white; } body { background-color: #fff; margin: 40px; font: 13px/20px normal Helvetica, Arial, sans-serif; color: #4F5155; } a { color: #003399; background-color: transparent; font-weight: normal; } h1 { color: #444; background-color: transparent; border-bottom: 1px solid #D0D0D0; font-size: 19px; font-weight: normal; margin: 0 0 14px 0; padding: 14px 15px 10px 15px; } code { font-family: Consolas, Monaco, Courier New, Courier, monospace; font-size: 12px; background-color: #f9f9f9; border: 1px solid #D0D0D0; color: #002166; display: block; margin: 14px 0 14px 0; padding: 12px 10px 12px 10px; } #container { margin: 10px; border: 1px solid #D0D0D0; -webkit-box-shadow: 0 0 8px #D0D0D0; } p { margin: 12px 15px 12px 15px; } </style> </head> <body> <div id="container"> <h1><?php echo $heading; ?></h1> <?php echo $message; ?> </div> </body> </html> |
Oke… Sudah selesai sekarang tinggal cara penggunaannya, contohnya begini :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Admin extends CI_Controller { function __construct() { parent::__construct(); // jangan lupa load helpernya $this->load->helper('error'); // jika tidak login if(!$this->session->userdata('login')){ show_401(); } } } |
Okey sekian tutorial saya tentang membuat custom error pada framework codeigniter, semoga bermanfaat, jika ada yang kurang jelas silahkan memberi tanggapan di bagian komentar, berikan juga +1 anda di tulisan ini
Kami (Catatan Belajar) juga hadir di Facebook, like kami ya klik tombol berikut:
Signature :
No copy paste content please…. If you want do that, please include the original source. Original Post by catatanbelajar.com, written by Ivo Idham Perdameian, Google plus profile.
after trying to get this working in all situations, I kind of gave up, but great work. Instead, I realized you can just customize errors/error_404.php and you can use <? $CI =& get_instance(); ?> to use all of codeigniter’s functions. So basically it is the same as having a controller. You can even test URL segments to make custom messages in the 404 page. Anyway, just thought I’d mention this way too since I found it not so bad.
I’m glad this can help you bro..