CodeIgniter Forums
Filter Cors now working - 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: Filter Cors now working (/showthread.php?tid=82262)



Filter Cors now working - Thiaxl - 06-26-2022

I have the following cors filter:

PHP Code:
...
public function 
before(RequestInterface $request$arguments null)
    {
        
        header
("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Headers: *");
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PATCH, PUT, DELETE");
        $method $_SERVER['REQUEST_METHOD'];
        if($method == "OPTIONS"){
            die();
        }
    }
... 

And in Filter class:

PHP Code:
...
public 
$aliases = [
        'csrf'          => CSRF::class,
        'toolbar'      => DebugToolbar::class,
        'honeypot'      => Honeypot::class,
        'invalidchars'  => InvalidChars::class,
        'secureheaders' => SecureHeaders::class,
        'authFilter'    => AuthFilter::class,
        'cors'          => CorsFilter::class,
    ];
... 

But not work.
It only works if I add the headers into index.php in the public folder.

How to resolve this?


RE: Filter Cors now working - includebeer - 07-02-2022

You just defined an alias, you're not calling you filter anywhere. That's why it's not working. You need to add the filter to the 'before' or 'after' array of the $globals property. See https://codeigniter.com/user_guide/incoming/filters.html#globals
In you case it would be in the 'before' array, like that:

PHP Code:
    public $globals = [
        'before' => [
            'cors',
        ],
        'after' => [],
    ]; 



RE: Filter Cors now working - InsiteFX - 07-02-2022

Donot us this in a live server!

Access-Control-Allow-Origin: *

Use:

Access-Control-Allow-Origin: https://yourDomain.com