The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

About optional segments?

Isidro A. López G.'s Avatar

Isidro A. López G.

09 Sep, 2015 07:31 PM

In nikic/FastRoute in Std class method parse our routes could be to an readable url for Slim core. But when I try to build something like this:
$app->get("/{module}/{controller}[/{action}][/{actionParams}][/{page}]", function($request, $response, $args) {
...
});

The parse function only remove from last segment the "]". In this case there are 3 optional segments.

Well I would like know how split all optional segments into an array to make the count, after register the routes?

  1. 1 Posted by Isidro A. López... on 09 Sep, 2015 07:40 PM

    Isidro A. López G.'s Avatar

    I forget say that all I want do with regular expressions where:
    1. Separate into two arrays the segments and optional segments.
    2. Count optional segments.
    3. Join segments and optional segments
    4. Do all stuff for parse function.

  2. 2 Posted by Isidro A. López... on 10 Sep, 2015 04:37 AM

    Isidro A. López G.'s Avatar

    Solution:
    I modified the FastRoute\RouteParser\Std::parse function.

    public function parse($route) {
            //$routeWithoutClosingOptionals = rtrim($route, ']');
            //$routeWithoutClosingOptionals = count(preg_split("/\[\/{/", $route)) - 1; // Count optional segments
            //$numOptionals = strlen($route) - strlen($routeWithoutClosingOptionals);
            $numOptionals = count(preg_split("/\[\/{/", $route)) - 1;

            // Split on [ while skipping placeholders
            //$segments = preg_split('~' . self::VARIABLE_REGEX . '(*SKIP)(*F) | \[~x', $routeWithoutClosingOptionals);
            $segments = preg_split('~' . self::VARIABLE_REGEX . '(*SKIP)(*F) | \[~x', $route);

            if ($numOptionals !== count($segments) - 1) {
                throw new BadRouteException("Number of opening '[' and closing ']' does not match");
            }
            //print_r($segments);die();
            $currentRoute = '';
            $routeDatas = [];
            foreach ($segments as $segment) {
                if ($segment === '') {
                    throw new BadRouteException("Empty optional part");
                }
                $segment = str_replace(']', '', $segment);
                $currentRoute .= $segment;
                $routeDatas[] = $this->parsePlaceholders($currentRoute);
            }
            //print_r($routeDatas);die();
            return $routeDatas;
        }

    Now I want implement Rob Allen skeleton where he uses actions stores in container dependencies. Still I cannot understood as work but now I'm researching. Any comment, suggest are welcome.

  3. Support Staff 3 Posted by Andrew Smith on 10 Sep, 2015 08:22 AM

    Andrew Smith's Avatar

    What you have proposed is not a solution, never modify library files as this will lead to more issues later down the line. Optionals work differently in FastRoute, you need to wrap all optionals inside of the first optional in order for it to work.

    You can see more on the solution here https://github.com/nikic/FastRoute/issues/55#issuecomment-113278913

    Please don't comment on that issue as its closed and solved, reply back here if you have any further questions.

  4. 4 Posted by Isidro A. López... on 11 Sep, 2015 02:38 PM

    Isidro A. López G.'s Avatar

    Thanks @AndrewSmith. Also recommend add this behaviour in the Slim documentation.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac