Use different 404 page on front-end and back-end |
Hi I'd like to ask a question please. I'm building a project in which I'm having this folder structure:
Code: application 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); 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 so when I check if the post exist, it would become like this PHP Code: if (count($data['post']) > 0) { 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.
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. 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. 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. So basically if I understand this correct, I can simply create: Code: application/controllers/admin/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. 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. (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. Correct. |
Welcome Guest, Not a member yet? Register Sign In |