Git Product home page Git Product logo

Comments (7)

stof avatar stof commented on July 28, 2024

Your patter is not a valid regexp.

from knpmenu.

Clindbergh avatar Clindbergh commented on July 28, 2024

What is wrong with the regexp?

However I think the issue persists.

from knpmenu.

Clindbergh avatar Clindbergh commented on July 28, 2024

When building the item as described above, \Knp\Menu\Integration\Symfony\RoutingExtension::buildOptions will add the route to the extras item.

The issue is in the following code in \Knp\Menu\Matcher\Voter\RouteVoter::matchItem:

       $routes = (array) $item->getExtra('routes', []);
// $routes is now [ pattern="my_route.*", 0 = [ route = "my_route", parameters= []]

        foreach ($routes as $testedRoute) {
// in the first iteration, $testedRoute is "my_reoute.*"
            if (\is_string($testedRoute)) {
// this will be true in the first iteration, thus the pattern 
// will be passed to isMatchingRoute as a route
                $testedRoute = ['route' => $testedRoute];
            }

            if (!\is_array($testedRoute)) {
                throw new \InvalidArgumentException('Routes extra items must be strings or arrays.');
            }

            if ($this->isMatchingRoute($request, $testedRoute)) {
                return true;
            }
        }

Subsequently in \Knp\Menu\Matcher\Voter\RouteVoter::isMatchingRoutethe pattern is never tested because$testedRoute['route']` is always set.

    private function isMatchingRoute(Request $request, array $testedRoute): bool
    {
        $route = $request->attributes->get('_route');
        if (isset($testedRoute['route'])) {
            if ($route !== $testedRoute['route']) {
                return false;
            }
        } elseif (!empty($testedRoute['pattern'])) {
            if (!\preg_match($testedRoute['pattern'], $route)) {
                return false;
            }

from knpmenu.

Clindbergh avatar Clindbergh commented on July 28, 2024

Above pull request will make a menu item with a pattern functional as below functional again:

            $menu->addChild('Location', [
                'route' => 'location_index', 
                'extras' => [ 
                    'routes' => [ 
                        'pattern' => '/location.*/'
                    ]
                ]
            ]);

(of course I forgot the slashes for the regex in my first comment)

from knpmenu.

stof avatar stof commented on July 28, 2024

Ah no, I found the issue. routes takes a list of routes to match, which can be either a string (for the shortcut case) or an associative array. Your config misses a level of array.

from knpmenu.

stof avatar stof commented on July 28, 2024

Compare your config with the the example you linked in the description.

from knpmenu.

Clindbergh avatar Clindbergh commented on July 28, 2024

Thanks 👍 , didn't spot that. This works now.

from knpmenu.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.