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

Optional routes doesn't work #11

Open
milosPavic opened this issue Sep 23, 2023 · 2 comments
Open

Optional routes doesn't work #11

milosPavic opened this issue Sep 23, 2023 · 2 comments

Comments

@milosPavic
Copy link

milosPavic commented Sep 23, 2023

Im running leaf mvc app, from public folder with command "leaf serve", im using PHP Version 7.4.24. on Windows 10

For example: Route with 2 optional parameters
app()->all('vest/{id}(/\w+)?(/\w+)?', "Api@vest");

IF i put all 3 parameters then works if i add 2 or 1 parameter in URL i get error:
trim() expects parameter 1 to be string, bool given

Core.php
if (isset($matches[$index + 1]) && isset($matches[$index + 1][0]) && is_array($matches[$index + 1][0])) {
return trim(substr($match[0][0], 0, $matches[$index + 1][0][1] - $match[0][1]), '/');
}

if i change code with this all is working fine
if (isset($matches[$index + 1]) && isset($matches[$index + 1][0]) && is_array($matches[$index + 1][0])) {
if(isset($match[$index+1][0])){
return trim($matches[$index + 1][0][0],'/');
}
}

@milosPavic
Copy link
Author

milosPavic commented Sep 24, 2023

Found error and I fix it in leaf Core.php private static function findRoute ...

        // Extract the matched URL parameters (and only the parameters)
            $params=[];
            foreach ($matches as $match){
                if($match[0][1]>=0){
                    $params[]=trim($match[0][0],'/');
                }
            }
            /*
            $params = array_map(function ($match, $index) use ($matches) {
                // We have a following parameter: take the substring from the current param position until the next one's position (thank you PREG_OFFSET_CAPTURE)
                if (isset($matches[$index + 1]) && isset($matches[$index + 1][0]) && is_array($matches[$index + 1][0])) {
                    $ret=substr($match[0][0], 0, $matches[$index + 1][0][1] - $match[0][1]);
                    if(!is_bool($ret)){
                        return trim($ret, '/');
                    }
                }
                // We have no following parameters: return the whole lot
                return isset($match[0][0]) ? trim($match[0][0], '/') : null;
            }, $matches, array_keys($matches));
            */

@milosPavic
Copy link
Author

Commented code makes problem, so when i use foreach loop and extract only parameters that is set in url all starts working fine.

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

1 participant