Welcome Guest, Not a member yet? Register   Sign In
404 Override Not Working
#11

(06-20-2017, 02:03 AM)wolfgang1983 Wrote:
(06-20-2017, 02:00 AM)Martin7483 Wrote: Viewing the Core code, setting a custom 404_override in ./config/routes.php does not change how the show_404() method is used.

The override is used for when your Controller can not be located. It will then load the Controller that is set in the config value.
This is not what is happening in your case, because CI can locate the controller.

Look at the docs https://codeigniter.com/user_guide/gener...l#show_404

What you need todo is create your own show_404 method or redirect to your custom Notfound controller instead of calling show_404 and pass along any needed arguments

Thanks for that will look into it.

You can easily test this.

Open ./system/core/CodeIgniter.php
Go to line 447 if($e404)
Place a die after the opening {
Navigate to a thread that can not be found via your Viewthread Controller
If it dies, then the required Controller could not be located. Else it was located and you need something other than show_404
Reply
#12

(06-20-2017, 02:05 AM)reactionstudio Wrote: If you create the file: application/core/MY_Exceptions.php with the following code what happens?

PHP Code:
<?php


class MY_Exceptions extends CI_Exceptions {

    function 
__construct() {

        
parent::__construct();

    }

    public function 
show_404($page ''$log_error TRUE)
    {
        if (
is_cli())
        {
            
$heading 'Not Found';
            
$message 'The controller/method pair you requested was not found.';
        }
        else
        {
            
$heading '404 Page Not Found';
            
$message 'The page you requested was not found.';
        }

        
        if (
$log_error)
        {
            
log_message('error'$heading.': '.$page);
        }
        
        if( 
is_cli() ) {

            echo 
$this->show_error($heading$message'error_404'404);

        } else {
            
header("HTTP/1.0 404 Not Found");
            
            
            
header('location: http://www.yoursite.com/notfound');

        }
        exit(
4); // EXIT_UNKNOWN_FILE
    
}



dont forget to replace www.yoursite.com with your applications domain. If it works you'll need to change how the base_url is passed in but this should be enough to see if it makes progress towards a solution.

Thanks that works also.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#13

(06-20-2017, 02:12 AM)wolfgang1983 Wrote:
(06-20-2017, 02:05 AM)reactionstudio Wrote: If you create the file: application/core/MY_Exceptions.php with the following code what happens?

PHP Code:
<?php


class MY_Exceptions extends CI_Exceptions {

 function 
__construct() {

 
parent::__construct();

 }

 public function 
show_404($page ''$log_error TRUE)
 {
 if (
is_cli())
 {
 
$heading 'Not Found';
 
$message 'The controller/method pair you requested was not found.';
 }
 else
 {
 
$heading '404 Page Not Found';
 
$message 'The page you requested was not found.';
 }

 
 if (
$log_error)
 {
 
log_message('error'$heading.': '.$page);
 }
 
 if( 
is_cli() ) {

 echo 
$this->show_error($heading$message'error_404'404);

 } else {
 
header("HTTP/1.0 404 Not Found");
 
 
 
header('location: http://www.yoursite.com/notfound');

 }
 exit(
4); // EXIT_UNKNOWN_FILE
 
}



dont forget to replace www.yoursite.com with your applications domain. If it works you'll need to change how the base_url is passed in but this should be enough to see if it makes progress towards a solution.

Thanks that works also.

This will work, but you are both missing the fact that the 404_override and show_404 are not directly related. Setting the one does not change the workings of the other. IMHO changing the show_404 to fix something that is not broken because of a lack of knowledge is not the way to do things.

Instead create a show_notfound() method that will handle the redirect to your custom Controller. This way you can keep using the show_404 as is, also within your custom Controller without causing an infinite redirect loop.
Reply
#14

(This post was last modified: 06-20-2017, 02:42 AM by reactionstudio.)

Martin7483 Wrote:What you need todo is create your own show_404 method or redirect to your custom Notfound controller instead of calling show_404 and pass along any needed arguments

Is this not what extending the core CI_Exceptions class is achieving? ok it's not a separate function, it's extending the core instead to get the same end result but i don't see how this is a bad solution just because it's not the one you would personally go with?

Please can you explain to me why it's a bad / incorrect solution?
Reply
#15

(06-20-2017, 02:37 AM)reactionstudio Wrote:
Martin7483 Wrote:What you need todo is create your own show_404 method or redirect to your custom Notfound controller instead of calling show_404 and pass along any needed arguments

Is this not what extending the core CI_Exceptions class is achieving? ok it's not a separate function, it's extending the core instead to get the same end result but i don't see how this is a bad solution just because it's not the one you would personally go with?

Please can you explain to me why it's a bad / incorrect solution?

First: I already did
Quote:This way you can keep using the show_404 as is, also within your custom Controller without causing an infinite redirect loop.

If the need should arise to call the show_404() method from within the custom Notfound Controller that would cause an infinite redirect loop. Meaning if he would need to call it he would have to again build something to overcome that issue.

Also any 404 unrelated to the forum he is building, will not be routed to the custom notfound controller

Second: Also already mentioned
You are “fixing something” that is not broken. How is that a correct approach to anything?
Reply
#16

Martin7483 Wrote:If the need should arise to call the show_404() method from within the custom Notfound Controller that would cause an infinite redirect loop. Meaning if he would need to call it he would have to again build something to overcome that issue.

This would be true if they did need to call the show_404() method within the Notfound Controller, which isn't a custom controller, they're extending the standard CI_Controller. From what they posted it does not seem like they would need to do this though as they were trying to set one controller for all 404 pages via $route['404_override'] = 'notfound'; which would suggest they only needed one 404 page, if they did then it's a different problem and a different solution is needed.

Martin7483 Wrote:Also any 404 unrelated to the forum he is building, will not be routed to the custom notfound controller

What if they only need one 404 page for their application? They didn't specify they needed a specific 404 page for their forum and a different one for other parts of their application, if this was needed though they could make use of the $page argument and redirect to the desired 404 page with a small modification to the code if it was required, again different problem, different solution.

Martin7483 Wrote:You are “fixing something” that is not broken. How is that a correct approach to anything?

I don't see how this is "fixing something that is not broken", it's changing how something works so it suits your specific needs.
Reply
#17

Wolfgang1983 assumed that setting the 404_override would effect the way show_404 is executed by CI. I have pointed out that this is not the case as the two are unrelated. Just take a look at CodeIgniter.php starting at line 447. Setting the 404_override only causes CI to load the custom 404 Controller instead of calling the show_404 method.

Quote:What if they only need one 404 page for their application?
Even if this is true, again, you are changing code to do something which can easily be done by creating a method specifically designed for the situation.

I like to think ahead when coding. So say, for now only one 404 page is needed. But in the future you do need one for the forum and one for any other part of the website. You would then need to undo the solution you have provided, and build a separate method as I have suggested. Or as you point out, do something based on the passed in page argument. But then you he would need to pass in the correct page argument to execute the correct 404.

Quote:I don't see how this is "fixing something that is not broken", it's changing how something works so it suits your specific needs.

Changing how something works indeed is not fixing something that is broken. But changing something based on a wrong interpretation of the Core code is not the correct approach.

PS. When I said custom Controller, I'm was talking about the custom 404 Controller Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB