Welcome Guest, Not a member yet? Register   Sign In
Convert Filters to Registrar
#1

Hello! 

I am having a little bit of trouble understanding the deprecation of Filters in favor of Registrars. I need some help in order to get my head around how Registrars work and what the pros are compared to the now deprecated Filters-way of doing things.
To convert my Filters to Registrar, I tried to follow the minimal instruction in the deprecated-note on the documentation-page. Since it does not cover much, I am not sure if I have done it correctly and what else I need to do. I have created a Registrar.php file in the Config-folder, where my filters are defined following the structure in the example:

Code:
<?php namespace App\Config;

use App\Filters\{AuthFilter, Cors, AdminFilter};

class Registrar {
  public static function Filters(): array {
    return [
      'aliases' => [
        'authFilter' => AuthFilter::class,
        'cors' => Cors::class,
        'adminFilter' => AdminFilter::class,
      ],
    ];
  }
}

I am a little unsure of what more I need to do, if anything? Should I change anything in the actual Filters-files? Can I remove the Filters from the $aliases list in Filters.php? Does it change the way I use the Filters in the Routes file?
I currently have three filters; Cors, AdminFilter and AuthFilter. These filters are being used before certain routes. For example, the AuthFilter makes sure a request is authenticated before letting the request through to its respective endpoint. My Cors filter handles Cors, and runs before any request. These filters work flawlessly with the now deprecated Filter-structure, but I do not get how I use them in my Routes the Registrars way. This is how my routes look now:

Code:
$routes->group('', ['filter' => 'cors'], static function (RouteCollection $routes): void {

  $routes->group('auth', static function ($routes) {
    $routes->options('login', 'Auth::login');
    $routes->post('login', 'Auth::login');
    $routes->options('refresh_token', 'Auth::refresh_token');
    $routes->get('refresh_token', 'Auth::refresh_token', ['filter' => 'authFilter']);
  });

  $routes->group('users', static function ($routes) {
    $routes->options('get_all_users', 'Users::get_all_users');
    $routes->get('get_all_users', 'Users::get_all_users', ['filter' => 'authFilter']);
    $routes->options('delete_user_by_username', 'Users::delete_user_by_username');
    $routes->delete('delete_user_by_username', 'Users::delete_user_by_username', ['filter' => 'adminFilter']);
    $routes->options('add_user', 'Users::add_user');
    $routes->post('add_user', 'Users::add_user', ['filter' => 'adminFilter']);
  });


I made these filters a while ago, so I was not aware of Codeigniters own Cors-filter. But now that I have looked into it, it also uses the Filter-way of doing things and not the Registrar-way. Filters was set to deprecated in 4.2, how come a new feature that is introduced in 4.5 use a deprecated way of doing things?
Reply
#2

What are you talking about?
Controller filters are not deprecated in 4.2.
https://codeigniter4.github.io/CodeIgnit...precations

Filters and Registrar are completely different things.
https://codeigniter4.github.io/CodeIgnit...lters.html
https://codeigniter4.github.io/CodeIgnit...registrars
Reply
#3

(05-02-2024, 05:16 PM)kenjis Wrote: What are you talking about?
Controller filters are not deprecated in 4.2.
https://codeigniter4.github.io/CodeIgnit...precations

Filters and Registrar are completely different things.
https://codeigniter4.github.io/CodeIgnit...lters.html
https://codeigniter4.github.io/CodeIgnit...registrars

It does say it is deprecated here: https://codeigniter4.github.io/CodeIgnit...ml#filters
Reply
#4

(05-03-2024, 12:16 AM)Willen Wrote: It does say it is deprecated here: https://codeigniter4.github.io/CodeIgnit...ml#filters

It is the documentation for how to register filter aliases from a module (or Composer package).
For example, Shield registers some filter aliases:
https://github.com/codeigniter4/shield/b...hp#L33-L48

Do you want to try to do something like that?
Reply
#5

(05-03-2024, 12:57 AM)kenjis Wrote:
(05-03-2024, 12:16 AM)Willen Wrote: It does say it is deprecated here: https://codeigniter4.github.io/CodeIgnit...ml#filters

It is the documentation for how to register filter aliases from a module (or Composer package).
For example, Shield registers some filter aliases:
https://github.com/codeigniter4/shield/b...hp#L33-L48

Do you want to try to do something like that?

My apologies then. I just saw the Filters header and that it said it was deprecated, didn't know it was only for registering filter aliases from a module (or Composer package). I guess then I can keep using the filters the way I have done before?
Reply
#6

Of course you can Wink
Simple CI 4 project for beginners codeigniter-expenses
Reply




Theme © iAndrew 2016 - Forum software by © MyBB