How to show 404 whenever/wherever I want |
Hello. In my site I have some pages that I only want a logged in administrator to see. If the admin is not logged in I don't want to show the typical login page or some error, I want to show error 404. In CodeIgniter 3 we could type show_404() wherever we wanted in the controller - and as soon as the code got to it - bam! 404. How can I do this in CI 4?
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
(04-15-2020, 08:04 AM)Leo Wrote: Hello. In my site I have some pages that I only want a logged in administrator to see. If the admin is not logged in I don't want to show the typical login page or some error, I want to show error 404. In CodeIgniter 3 we could type show_404() wherever we wanted in the controller - and as soon as the code got to it - bam! 404. How can I do this in CI 4? I believe this should meet ... PHP Code: if //anything you want
That code does't work for me. The whole syntax
return redirect()->to('/wherever'); is not working for me, at least not in that controller. But even if it did wouldn't it just redirect to /errors/nodata ?? I don't want to redirect though. I just want to show 404 page not found with the uri they were trying to access. Scenario: They are an unauthorized user probing through my site, theres a url called /edit_products - that should only be viewable by admins. If they are unauthorized as an admin they see 404 on that link.
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
(04-15-2020, 10:19 AM)Leo Wrote: That code does't work for me. The whole syntax There are options through .htaccess, also. /errors/nodata => It was just an example, I don't know if 404 routing is customized by you. I warned you that my 404 page is customized. I changed the router configuration in app/config/routes.php PHP Code: $routes->setDefaultNamespace('App\Controllers');
Thanks, I made a custom 404 too with your router example.
But still, how do I show some users 404 and others the real content - without changing the url... .htaccess options? What kind of options? ![]()
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Once you've customised your response, can't you simply invoke it by using 'throw'?
(04-15-2020, 12:29 PM)Gary Wrote: Once you've customised your response, can't you simply invoke it by using 'throw'?Errr...that sounds promising. Customize response and throw it..I will try it when I figure out how...
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
@gary's suggestion can help:
https://codeigniter.com/user_guide/incom...llers.html https://codeigniter.com/user_guide/incom...lters.html
https://codeigniter.com/user_guide/gener...exceptions
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('WTF'); (04-15-2020, 01:13 PM)Gary Wrote: https://codeigniter.com/user_guide/gener...exceptionsGreat! Meanwhile I was reading this... https://codeigniter.com/user_guide/outgo...ponse.html, and completely missed error handling of the guide. Thank YOU kind sir! ![]()
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
|
Welcome Guest, Not a member yet? Register Sign In |