Welcome Guest, Not a member yet? Register   Sign In
Before filters with main page redirect when I call a controller with ajax
#1

(This post was last modified: 11-23-2022, 03:31 AM by serialkiller.)

I created a filter which I added to the before list in the Config/Filters.php file, added some exceptions and everything works.

My application in several parts uses ajax to load content into the page, for example lists of records, or send data from forms.
In these cases the filter continues to work but the redirect is not done on the main page, but in the block where the content is loaded via ajax.
This all makes sense, because I call that controller and that controller is affected by the filter rules.

Is there a way to redirect the main page and not the ajax block?
Reply
#2

Try exit
Reply
#3

(11-23-2022, 02:23 PM)motoroller Wrote: Try exit

Try to explain
Reply
#4

Code example? Where redirect filter
Reply
#5

My filter

PHP Code:
namespace App\Filters;

use 
CodeIgniter\Filters\FilterInterface;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;

class 
Is_logged implements FilterInterface
{
    public function before(RequestInterface $request$arguments null)
    {
        $model model('App\Models\Utility\Check');


        if (!$model->my_check()) : // return true or false

            return redirect()->to('/');

        endif;
    }


    public function after(RequestInterface $requestResponseInterface $response$arguments null)
    {
    }


As said, it works, the problem is in the ajax calls, I know that the behavior is logical, I would need to redirect the whole page and not just the block where ajax writes
Reply
#6

(This post was last modified: 11-24-2022, 11:39 AM by ozornick.)

Do you want an unauthorized user to be redirected to the main page when requesting Ajax?
then you need to determine that the request is from XHR and return some status or link. In JS function check "url = redirect or html = paste on page" condition
https://codeigniter.com/user_guide/general/ajax.html
Reply
#7

(11-24-2022, 11:37 AM)ozornick Wrote: Do you want an unauthorized user to be redirected to the main page when requesting Ajax?
then you need to determine that the request is from XHR and return some status or link. In JS function check "url = redirect or html = paste on page" condition
https://codeigniter.com/user_guide/general/ajax.html

Let me explain, I'm migrating a big application from CI3 to CI4, I'm practically done, I'm trying to fix some things that can still be improved with the new features of CI4.
One of these are filters and the use of before and after.
I already have the same control managed by javascript, but I wanted to take advantage of the filters also avoiding extra http calls.
The behavior must exist both on "normal" calls, and in ajax calls.
If there's no way to tell php/CI to redirect the entire page and not just the ajax-handled portion, I'll stick with the current pure javascript version.
Passing a parameter would also be feasible, but some calls only return html and it would be problematic to modify the whole structure, thanks anyway for the advice, but at this point I think I'll stay with the javascript version
Reply




Theme © iAndrew 2016 - Forum software by © MyBB