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

How to get args for is_current_url()? #187

Closed
badmansan opened this issue Jul 8, 2020 · 2 comments
Closed

How to get args for is_current_url()? #187

badmansan opened this issue Jul 8, 2020 · 2 comments

Comments

@badmansan
Copy link

Good day. The code:
router:

$app->get('/user/view/{id}', UserView::class)->setName('user.view');

twig:

<li class="{{ is_current_url('user.view') ? 'active' : '' }}">

The problem for is_current_url is in missing parameter id. In this case we get Missing data for URL segment: id error. How can i check if rule user.view is current url?

@l0gicgate
Copy link
Member

You need to pass in the data parameters to is_current_url. Example if the url is /user/view/1 you'd need to do is_current_url('user.view', ['id' => 1]) which would return true.

@badmansan
Copy link
Author

The result solution:
middleware:

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $routeContext = RouteContext::fromRequest($request);
        $route = $routeContext->getRoute();

        if ($route === null) {
            return $handler->handle($request);
        }

        $this->twig->getEnvironment()->addGlobal('route_name', $route->getName());

        return $handler->handle($request);
    }

twig:

<li class="{{ route_name == 'root.page' ? 'active' : '' }}">
<li class="{{ route_name in ['user.add', 'user.view', 'user.delete'] ? 'active' : '' }}">

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