• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codeigniter-4 redirect back and ajax issue

#1
Does anyone have a suggestion on how to fix this issue without manually modifying CI core code.

CI4 saves a reference to the last requested url so it can do it's redirect()->back() function however when you use ajax it stores that call and when I want to use back on the next page (say a link clicked from the current page) it tries to redirect me back to the ajax page and not the main page.

CI's function that handles this is in system/Codeigniter.php (line 969 on my version) public function storePreviousURL($uri)

I can add an exception inside of this to ignore url's with ajax in the name however I am editing code I shouldn't be touching because it will get overwritten during updates. I am not sure how to intercept this and make changes from the outside. I am not sure how to override this class since it's a level above the core classes that you can extend and override.

Thanks for the help.
Reply

#2
You can create a class that extends to the RedirectResponse.php class in your system/HTTP/RedirectResponse.php and re-write your back() function that way. Leaving the system library untouched.

Wait, actually I just checked the method you mention and the ajax methods should be ignore by default.



PHP Code:
public function storePreviousURL($uri)
 {
   
// Ignore CLI requests
   
if (is_cli())
   {
      return;
   }
   
// Ignore AJAX requests
   
if (method_exists($this->request'isAJAX') && $this->request->isAJAX())
   {
      return;
   }
.....

Reply

#3
(07-03-2020, 01:19 AM)Thank you, it seems that when i did my composer update it didn\t actually update my files past 4.0 and it said there were no updateds found but when I downloaded the latest code copied the new files over my old ones it worked. Not sure why composer isn't working I'll have to try and figure that out another time.marcogmonteiro Wrote: You can create a class that extends to the RedirectResponse.php class in your system/HTTP/RedirectResponse.php and re-write your back() function that way. Leaving the system library untouched.

Wait, actually I just checked the method you mention and the ajax methods should be ignore by default.



PHP Code:
public function storePreviousURL($uri)
 {
   // Ignore CLI requests
   if (is_cli())
   {
      return;
   }
   // Ignore AJAX requests
   if (method_exists($this->request'isAJAX') && $this->request->isAJAX())
   {
      return;
   }
.....

Reply

#4
Cool, that means there's no issue with the redirect thing Smile
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.