CodeIgniter Forums
Codeigniter-4 redirect back and ajax issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Codeigniter-4 redirect back and ajax issue (/showthread.php?tid=76915)



Codeigniter-4 redirect back and ajax issue - Cyto5 - 07-02-2020

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.


RE: Codeigniter-4 redirect back and ajax issue - marcogmonteiro - 07-03-2020

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;
   }
.....




RE: Codeigniter-4 redirect back and ajax issue - Cyto5 - 07-05-2020

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




RE: Codeigniter-4 redirect back and ajax issue - marcogmonteiro - 07-06-2020

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