-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
docs: add Getting Routing Information #9129
docs: add Getting Routing Information #9129
Conversation
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 <[email protected]>
I would expand the information about the routes. How to get the router parameters ( |
@datamweb @neznaika0 How about this? Adding a new section "Getting Routing Information".
|
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
|
@neznaika0 Why do you want to get route options (name, filters, priority...) ? |
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)) {
return redirect()->to('/login');
} |
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 <[email protected]>
@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'] ?? ''; |
@neznaika0 I still don't understand your use case completely. Where do you put the code like that? |
Example: if (! empty($routeName) && $routeName === 'homepage') {
throw new LogicException('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) |
I still don't understand why you need such code? |
Co-authored-by: kenjis <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thank you.
@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') {
// better
if (! empty($routeName) && $routeName === 'homepage') {
throw new LogicException('Access control: Found redirect loop');
} |
@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: