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

docs: add Getting Routing Information #9129

Merged

Conversation

datamweb
Copy link
Contributor

@datamweb datamweb commented Aug 17, 2024

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.

  • Retrieving the Controller and Method Names
  • Accessing Active Filters in Router

Screenshot 2024-08-19 180907
Screenshot 2024-08-19 181039

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added the documentation Pull requests for documentation only label Aug 18, 2024
@kenjis
Copy link
Member

kenjis commented Aug 18, 2024

I'm not sure that is the right place for this section.
Isn't it better to move it to the Controllers page?
Screenshot 2024-08-18 11 36 13

@datamweb
Copy link
Contributor Author

I'm not sure that is the right place for this section.
Isn't it better to move it to the Controllers page?

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?

@neznaika0
Copy link
Contributor

I would expand the information about the routes. How to get the router parameters (name, filters, priority...)
Now there are no ready-made methods for this, only an array

@kenjis
Copy link
Member

kenjis commented Aug 19, 2024

@datamweb @neznaika0 How about this?
Screenshot 2024-08-19 17 20 13

Adding a new section "Getting Routing Information".
Because

  • This topic is about Routing (Router). So it is better to be in the Routing page.
  • The existing sections are all for how to set routes. But this is how to get the routing info.

@neznaika0
Copy link
Contributor

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

public function getRoutesOptions(?string $from = null, ?string $verb = null): array

@kenjis
Copy link
Member

kenjis commented Aug 19, 2024

@neznaika0 Why do you want to get route options (name, filters, priority...) ?
Use case?

@datamweb
Copy link
Contributor Author

  • This topic is about Routing (Router). So it is better to be in the Routing page.
  • The existing sections are all for how to set routes. But this is how to get the routing info.

Your reasons are quite fair and have explained the issue well.

get route options (name, filters, priority...) ?
Use case?

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');
}

@datamweb
Copy link
Contributor Author

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?

@neznaika0
Copy link
Contributor

@kenjis Many people use the URL as a condition for an action/redirect. I used to rely on built-in functions with routes (routeName). It's more reliable.

$router      = Services::router();
$routeName   = $router->getMatchedRouteOptions()['as'] ?? '';

@datamweb datamweb changed the title docs: add Retrieving the Controller and Method Names for the Current Route docs: add Getting Routing Information Aug 19, 2024
@kenjis
Copy link
Member

kenjis commented Aug 20, 2024

@neznaika0 I still don't understand your use case completely. Where do you put the code like that?

@neznaika0
Copy link
Contributor

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)

@kenjis
Copy link
Member

kenjis commented Aug 20, 2024

I still don't understand why you need such code?
Where do you put it?

Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you.

@neznaika0
Copy link
Contributor

@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');
}

@kenjis
Copy link
Member

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.
But this is an enhancement.

@kenjis kenjis merged commit 655bd1d into codeigniter4:develop Aug 22, 2024
9 checks passed
@datamweb datamweb deleted the docs-add-Retrieving-Controller-MethodNames branch August 22, 2024 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Pull requests for documentation only
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants