CodeIgniter Forums

Full Version: Use different 404 page on front-end and back-end
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi I'd like to ask a question please. I'm building a project in which I'm having this folder structure:

Code:
application
- controllers
-- posts.php // this is on frontend
-- admin
--- posts.php // this is on backend obviously

So in both cases if I try to access from the url a post that doesn't exist I'll do something like this

PHP Code:
$data['post'] = $this->post_model->get_by_id($id);
// Return 404 if post not found
if (count($data['post']) > 0) {
      
// do something ...
} else {
    
show_404();


which shows the default 404 Codeigniter page.

Is it possible to set a different 404 page for front end and back end and if so how can I do this ?

The only options I have thought so far are these:

1st. Have the same custom 404 page for both front end and back end, in which case I only have to set a different 404 view to the default file, but I don't think its the best option.

2nd (not tested). Create 2 different controllers and views, like this
PHP Code:
applicatiom
controllers
-- front404.php
-- admin
--- admin404.php 

so when I check if the post exist, it would become like this

PHP Code:
if (count($data['post']) > 0) {
      
// do something ...
} else {
        
// redirect to front 404 or admin 404 depend on whether I'm on public site or on the admin area
    
redirect('admin/admin404'); // so on front end it would be redirect('admin/front404'); instead


3rd (also not test). A similar to 2nd option, but setting the $route['404_override'] = ''; dynamically. Would that be possible ??

However I was wondering if there's a more efficient way of doing this. Btw I'm using CodeIgniter v3.
I would set a 404_override controller and handle the front- and back end 404 in that controller.

I don't think you can set the 404_override dynamically because the uri segments are set in the Router. You would need to extend the core Router if you want a frontend_404 and an admin_404
If your admin post does not exist, redirect as you did.
  1. create Admin404.php file in /application/controllers/admin/ folder.
  2. make class Admin404 in Admin404.php file
  3. create index() function in Admin404 class
  4. call your custom admin 404 error view in index() function with
    PHP Code:
    $this->load->view('admin/error_404'); 
  5. create error_404.php view file in /application/views/admin/ and put your custom stuff in it.
That is it. It should work like you want.
You can't have different 404 pages if you just call show_404() ... it's obviously the same function no matter what you do.

(09-28-2015, 04:35 AM)Martin7483 Wrote: [ -> ]I would set a 404_override controller and handle the front- and back end 404 in that controller.

I don't think you can set the 404_override dynamically because the uri segments are set in the Router. You would need to extend the core Router if you want a frontend_404 and an admin_404

Actually, it's quite the opposite ... 404_override may be a single value, but it points at what to look for in the current directory and then naturally falls back to the root one.

When you have application/controllers/Error404.php and application/controllers/admin/Error404.php, setting $route['404_override'] to 'Error404' will cause admin/Error404.php to be loaded if you visit /admin/non-existing-uri.

I do have to say this though - you shouldn't 404 based on whether items exist in a list or not. The list itself still exists, even if empty.
(09-28-2015, 06:09 AM)Narf Wrote: [ -> ]Actually, it's quite the opposite ... 404_override may be a single value, but it points at what to look for in the current directory and then naturally falls back to the root one.

When you have application/controllers/Error404.php and application/controllers/admin/Error404.php, setting $route['404_override'] to 'Error404' will cause admin/Error404.php to be loaded if you visit /admin/non-existing-uri.

Did not know that, and I believe this is not explained in the user guide
(09-28-2015, 06:09 AM)Narf Wrote: [ -> ]Actually, it's quite the opposite ... 404_override may be a single value, but it points at what to look for in the current directory and then naturally falls back to the root one.

When you have application/controllers/Error404.php and application/controllers/admin/Error404.php, setting $route['404_override'] to 'Error404' will cause admin/Error404.php to be loaded if you visit /admin/non-existing-uri.

I do have to say this though - you shouldn't 404 based on whether items exist in a list or not. The list itself still exists, even if empty.

So basically if I understand this correct, I can simply create:

Code:
application/controllers/admin/Error404.php

application/controllers/Error404.php

and set in my routes $route['404_override'] = 'Error404';

So basically with this setting if I'm on public site and visit my-site.com/non-existing-uri,  I 'll be redirected to Error404, whereas if I access
my-site.com/admin/non-existing-uri, I will be redirected to admin/Error404, am I right ???
(09-28-2015, 06:33 AM)Martin7483 Wrote: [ -> ]
(09-28-2015, 06:09 AM)Narf Wrote: [ -> ]Actually, it's quite the opposite ... 404_override may be a single value, but it points at what to look for in the current directory and then naturally falls back to the root one.

When you have application/controllers/Error404.php and application/controllers/admin/Error404.php, setting $route['404_override'] to 'Error404' will cause admin/Error404.php to be loaded if you visit /admin/non-existing-uri.

Did not know that, and I believe this is not explained in the user guide

Indeed it wasn't ... https://github.com/bcit-ci/CodeIgniter/c...f694fa3907

But actually, I also said something that's not entirely true:

(09-28-2015, 06:09 AM)Narf Wrote: [ -> ]You can't have different 404 pages if you just call show_404() ... it's obviously the same function no matter what you do.

It's the same function, and you can't change the function itself, but you can modify the error template to display something different based on the first URI segment. Smile

(09-28-2015, 06:51 AM)Lykos22 Wrote: [ -> ]
(09-28-2015, 06:09 AM)Narf Wrote: [ -> ]Actually, it's quite the opposite ... 404_override may be a single value, but it points at what to look for in the current directory and then naturally falls back to the root one.

When you have application/controllers/Error404.php and application/controllers/admin/Error404.php, setting $route['404_override'] to 'Error404' will cause admin/Error404.php to be loaded if you visit /admin/non-existing-uri.

I do have to say this though - you shouldn't 404 based on whether items exist in a list or not. The list itself still exists, even if empty.

So basically if I understand this correct, I can simply create:


Code:
application/controllers/admin/Error404.php

application/controllers/Error404.php

and set in my routes $route['404_override'] = 'Error404';

So basically with this setting if I'm on public site and visit my-site.com/non-existing-uri, I 'll be redirected to Error404, whereas if I access
my-site.com/admin/non-existing-uri, I will be redirected to admin/Error404, am I right ???

Correct.
Narf, thank you. It is good to know.