Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Discover filters not working #9455

Closed
Franky5831 opened this issue Feb 20, 2025 · 2 comments
Closed

Bug: Discover filters not working #9455

Franky5831 opened this issue Feb 20, 2025 · 2 comments

Comments

@Franky5831
Copy link

PHP Version

8.3

CodeIgniter4 Version

4.6.0

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

macOS

Which server did you use?

fpm-fcgi

Database

No response

What happened?

I am creating a composer package to be used in future CI4 projects (and to make opensource once all the features have been developed).

The CodeIgniter\Router\RouteCollection class has a method called discoverRoutes which looks for files named Config/Routes.php in your CI4 project (both in the App directory and in vendor).

The CodeIgniter\Filters\Filters class has a very similar method called discoverFilters which does the same thing but with files named Config/Filters.php, on line 187 it checks if the name of the class it has found is FiltersConfig::class, this way it does not include the Filters class from the App directory again.

// The $file may not be a class file.
 $className = $locator->getClassname($file);

// Don't include our main Filter config again...
if ($className === FiltersConfig::class || $className === BaseFiltersConfig::class) {
    continue;
}

With this approach it is not initializing the Filters class from my package, they have the same class name but different namespaces, I think it would be better to check if the class has the same class name and namespace as the FiltersConfig class.

Steps to Reproduce

  1. Create a directory in vendor
  2. Create your composer.json file, this is an example:
{
    "name": "add a name",
    "type": "package",
    "autoload": {
        "psr-4": {
            "YourVendor\\PackageName\\": "src/"
        }
    },
    "require": {
        "php": "^8.1",
        "codeigniter4/framework": "^4.0"
    },
}
  1. create a src directory inside
  2. inside that directory create Config/Filters.php
  3. create the Filters class => it will not be included from discoverFilters
  4. keep the file name but delete the class and make it into a regular .php file => it will be included from discoverFilters

I found a workaround using the config function:

config('Filters')->aliases["redirect_to_login"] = class name was here;
config('Filters')->globals["before"][] = "redirect_to_login";

Expected Output

.

Anything else?

No response

@Franky5831 Franky5831 added the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 20, 2025
@michalsn
Copy link
Member

Using filters in this form is deprecated: https://codeigniter.com/user_guide/general/modules.html#filters

Creating an additional Filters class was never an option (file - yes, but not a class). The Filters class is one and exists in app/Config/Filters.php.

You can add aliases via Registrars, but other changes should be done directly in the config Filters class.

@michalsn michalsn removed the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 20, 2025
@Franky5831
Copy link
Author

Thanks @michalsn, now it's all more clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants