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: Route that does not exist (404) does not trigger the 'after' part in required filters #9292

Open
Sunchock opened this issue Nov 28, 2024 · 3 comments · May be fixed by #9298
Open

Bug: Route that does not exist (404) does not trigger the 'after' part in required filters #9292

Sunchock opened this issue Nov 28, 2024 · 3 comments · May be fixed by #9298
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@Sunchock
Copy link

Sunchock commented Nov 28, 2024

PHP Version

8.3

CodeIgniter4 Version

4.5.5

CodeIgniter4 Installation Method

Composer (as dependency to an existing project)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

cli-server (PHP built-in webserver)

Database

MySQL 8.0.30

What happened?

I created a filter that should be run before and after each call to the application.

I added it to the "before" and "after" sections of the required filters in the Config/Filters.php file.

But when I try to call my application with a route that doesn't exist, only the 'before' part is executed.

Steps to Reproduce

1 - Create a filter and add it to required filters (both before & after)
2 - Call your app with an undefined route (e.g. GET http://myci4app/test)

Expected Output

I expect the after function to be called even if the route does not exist as suggested in the documentation

Anything else?

I'm in development ENVIRONMENT with cli-server for development purposes.
I didn't test this bug on another environment

According to my investigations:

The exception is thrown in Router.php L:222 in the "if no automatic routing" block. Then it goes to the display404errors function which raises another exception which is handled in CodeIgniter\Debug\Exceptions::exceptionHandler, causing it to lose the execution line in the main 'run' function.

@Sunchock Sunchock added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 28, 2024
@Sunchock Sunchock changed the title Bug: Route that does not exist (404) does not trigger the required filters Bug: Route that does not exist (404) does not trigger the 'after' part in required filters Nov 28, 2024
@michalsn
Copy link
Member

michalsn commented Nov 29, 2024

The claim from the user guide (https://codeigniter.com/user_guide/incoming/filters.html#required):

The Required Filters are always executed even if a route does not exist.

is a bit surprising to me. I guess it should be changed to:

The Required Filters are always executed. If a route does not exist, only the Before filter is executed.

But I will ask @kenjis what the intentions were here.

@neznaika0
Copy link
Contributor

we discussed a similar issue here
https://forum.codeigniter.com/showthread.php?tid=86691

@michalsn michalsn linked a pull request Dec 2, 2024 that will close this issue
5 tasks
@michalsn
Copy link
Member

michalsn commented Dec 2, 2024

Based on how everything has always worked and the forum conversations, I believe everything is ok. I have sent a PR that will fix the note in the user guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants