Welcome Guest, Not a member yet? Register   Sign In
How to show 404 whenever/wherever I want
#1
Question 

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.
Reply
#2

(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. 
Reply
#3

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.
Reply
#4

(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); 
Reply
#5

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
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply
#6

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

(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.
Reply
#8

(This post was last modified: 04-15-2020, 12:40 PM by wdeda.)

@gary's suggestion can help:

  https://codeigniter.com/user_guide/incom...llers.html

https://codeigniter.com/user_guide/incom...lters.html
Reply
#9

(This post was last modified: 04-15-2020, 01:25 PM by Gary.)

https://codeigniter.com/user_guide/gener...exceptions

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

(This post was last modified: 04-15-2020, 01:24 PM by Leo.)

(04-15-2020, 01:13 PM)Gary Wrote: https://codeigniter.com/user_guide/gener...exceptions
 Great! 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!  Big Grin
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB