Slim 3 - 斜线作为路径参数的一部分

时间:2016-08-29 10:10:03

标签: php regex slim slim-3

我需要使用包含斜杠/的参数来组合URL。例如,经典的/hello/{username}路线。默认情况下,/hello/Fabien将匹配此路线,但不匹配/hello/Fabien/Kris。我想问你怎么能在Slim 3框架中做到这一点。

2 个答案:

答案 0 :(得分:5)

Route placeholders

  

对于“无限制”可选参数,您可以这样做:

public static void main(String[] args)
        {
//      The data
        int[] numbers = {1, 2, 3, 4, 5};
        String[] words = {"6", "7", "8", "9", "10"};

    //      Create a new tempNumber array with word length
            int[] tempNumber = new int[words.length];

    //      Set i=0 just for a shorter for loop
            int i = 0;
    //      Enter each string to the tempNumber array.
            for( String s : words )
            {
                tempNumber[i++] = Integer.valueOf(s);
            }

    //      Set i to - so it can be reused.
            i = 0;

    //      Create a new tempNumber array with number length
            String[] tempString = new String[numbers.length];
    //      Enter each int to the tempNumber array
            for( int n : numbers )
            {
                tempString[i++] = String.valueOf(n);
            }

    //      Set numbers array to the one we created.
            numbers = tempNumber;
    //      Set string Array to the one we created;
            words = tempString;

    //      Output the result
            System.out.println("The numbers array has: "+Arrays.toString(numbers));
            System.out.println("The string array has: "+Arrays.toString(words));

    //      Output~~~~~~~~~~~~~~~~
    //      The numbers array has: [6, 7, 8, 9, 10]
    //      The string array has: [1, 2, 3, 4, 5]
        }

答案 1 :(得分:0)

您也可以使用$args

$app->get('/hello[/{route:.*}]', function ($request, $response, $args) {
    $route = $args['route']; // Whole Route
    $params = explode('/', $route); // Route split
});