Uh oh!
There was an error while loading. Please reload this page.
docs: add Getting Routing Information - #9129
Conversation
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
datamweb
commented
Aug 18, 2024
Honestly, I’m not entirely sure which section would be the most appropriate. However, let me explain why I initially placed this under the Specifying Route Handlers section within the URI Routing page. This section focuses on different ways to specify what code should handle a particular route. Since retrieving the controller and method names is directly related to how route handlers are resolved by the framework, I felt that placing it here made sense. The Specifying Route Handlers section already covers topics such as controllers, closures, and array callables, which are different methods for defining route handlers. Including information on retrieving the controller and method names could add valuable context to this section, as it helps in understanding how route handlers are determined and processed. That said, I’m open to moving this content to another section. Could you please specify the exact location of the Controllers page so I can make the necessary changes? |
Co-authored-by: kenjis <kenji.uui@gmail.com>
neznaika0
commented
Aug 19, 2024
I would expand the information about the routes. How to get the router parameters ( |
Uh oh!
There was an error while loading. Please reload this page.
kenjis
commented
Aug 19, 2024
@datamweb@neznaika0 How about this? Adding a new section "Getting Routing Information".
|
neznaika0
commented
Aug 19, 2024
Yes. The new section looks better. I've talked about it, there's no way to get the route details. You need to parse it yourself |
kenjis
commented
Aug 19, 2024
@neznaika0 Why do you want to get route options (name, filters, priority...) ? |
datamweb
commented
Aug 19, 2024
Your reasons are quite fair and have explained the issue well.
For example(filters), if an authentication filter is not active, you might redirect the user to a login page: $router = service('router');
$filters = $router->getFilters();
if (!in_array('auth', $filters)) {
returnredirect()->to('/login');
} |
datamweb
commented
Aug 19, 2024
I would like to add a section called Accessing Active Filters in Router to Getting Routing Information as well. Do you agree with this addition? |
Co-authored-by: kenjis <kenji.uui@gmail.com>
neznaika0
commented
Aug 19, 2024
@kenjis Many people use the URL as a condition for an action/redirect. I used to rely on built-in functions with routes ( $router = Services::router();
$routeName = $router->getMatchedRouteOptions()['as'] ?? ''; |
kenjis
commented
Aug 20, 2024
@neznaika0 I still don't understand your use case completely. Where do you put the code like that? |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
neznaika0
commented
Aug 20, 2024
Example: if (! empty($routeName) && $routeName === 'homepage') {
thrownewLogicException('Access control: Found redirect loop');
}It doesn't matter where - it's more reliable than a URL comparison. I use in my "access control" based on controllers (not as Shield) |
kenjis
commented
Aug 20, 2024
I still don't understand why you need such code? |
Co-authored-by: kenjis <kenji.uui@gmail.com>
neznaika0
commented
Aug 21, 2024
@kenjis It's in the filter now. It doesn't matter. The main reason is not to compare dynamic URLs, but a constant route name. // not// if (base_url('main') === 'http://example.com/main') {// betterif (! empty($routeName) && $routeName === 'homepage') {
thrownewLogicException('Access control: Found redirect loop');
} |
kenjis
commented
Aug 21, 2024
@neznaika0 Okay, thank you for your explanation. In my opinion, it is better that the Request (IncomingRequest) has the current route info including the route name. |


Description
I was unable to find clear documentation on how to retrieve the controller and method names for the current route in CodeIgniter 4. After searching through the user guide without success, I had to read the source code to figure out the correct approach. While I'm not entirely sure if this has been documented elsewhere, I believe this addition will be helpful for developers who need to dynamically interact with the controller or method handling the current request.
Checklist: