Welcome Guest, Not a member yet? Register   Sign In
Filter Cors now working

(This post was last modified: 06-26-2022, 01:21 PM by Thiaxl.)

I have the following cors filter:

PHP Code:
public function 
before(RequestInterface $request$arguments null)
("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"){

And in Filter class:

PHP Code:
$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?

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/incom...ml#globals
In you case it would be in the 'before' array, like that:

PHP Code:
    public $globals = [
        'before' => [
        'after' => [],
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/

Donot us this in a live server!

Access-Control-Allow-Origin: *


Access-Control-Allow-Origin: https://yourDomain.com
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )

Theme © iAndrew 2016 - Forum software by © MyBB