CodeIgniter Forums
How to show 404 whenever/wherever I want - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: How to show 404 whenever/wherever I want (/thread-76123.html)



How to show 404 whenever/wherever I want - Leo - 04-15-2020

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?


RE: How to show 404 whenever/wherever I want - wdeda - 04-15-2020

(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
    {
 return 
redirect()->to('/errors/nodata');
 exit;
 }
 
 
//my 404 page is customized. 



RE: How to show 404 whenever/wherever I want - Leo - 04-15-2020

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.


RE: How to show 404 whenever/wherever I want - wdeda - 04-15-2020

(04-15-2020, 10:19 AM)Leo Wrote: 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.

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');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override(function()  // <= changed
{
    $header = array (
    'icon' => 'favicon',
    'css' => 'nodata',
    'title' => 'Page not found - wdeda',
    'placeholder' => 'Search'
    );
    
    
echo view('templates/headerr'$header);
    echo view('/content/error_404');
    echo view('templates/footer');
    });
$routes->setAutoRoute(true); 



RE: How to show 404 whenever/wherever I want - Leo - 04-15-2020

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? Smile


RE: How to show 404 whenever/wherever I want - Gary - 04-15-2020

Once you've customised your response, can't you simply invoke it by using 'throw'?


RE: How to show 404 whenever/wherever I want - Leo - 04-15-2020

(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...


RE: How to show 404 whenever/wherever I want - wdeda - 04-15-2020

@gary's suggestion can help:

  https://codeigniter.com/user_guide/incoming/controllers.html

https://codeigniter.com/user_guide/incoming/filters.html


RE: How to show 404 whenever/wherever I want - Gary - 04-15-2020

https://codeigniter.com/user_guide/general/errors.html?highlight=throw%20exception#custom-exceptions

throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('WTF');


RE: How to show 404 whenever/wherever I want - Leo - 04-15-2020

(04-15-2020, 01:13 PM)Gary Wrote: https://codeigniter.com/user_guide/general/errors.html?highlight=throw%20exception#custom-exceptions
 Great! Meanwhile I was reading this... https://codeigniter.com/user_guide/outgoing/response.html, and completely missed error handling of the guide.

Thank YOU kind sir!  Big Grin