SlimFramework v3 - 与AND whithout final'/'相同的路线

时间:2016-11-17 11:28:08

标签: php slim

我使用SlimFramework v3创建了一个小应用程序,我可以构建一个这样的简单路径:

.table_truck .truck_cont{
    height: 85%;
    vertical-align:middle;
    display: flex; /* To use flexbox I need to set a flex display */
    flex-direction: column; /* to make it vertical I change the direction to column */
    justify-content: center; /* finally, I must set the orientation to center */
}

我的问题是,这只适用于 localhost / admin ,而不适用于 localhost / admin / (带有最终反斜杠)。是否可以选择使用 ONE 路线?

1 个答案:

答案 0 :(得分:1)

有两种可能性

  1. 指定可选的/

    $app->get('/admin[/]', function(){
        # code here
    });
    
  2. 添加中间件,将带有结尾/的路由重定向到没有它的网址。

    use Psr\Http\Message\RequestInterface as Request;
    use Psr\Http\Message\ResponseInterface as Response;
    
    $app->add(function (Request $request, Response $response, callable $next) {
        $uri = $request->getUri();
        $path = $uri->getPath();
        if ($path != '/' && substr($path, -1) == '/') {
            // permanently redirect paths with a trailing slash
            // to their non-trailing counterpart
            $uri = $uri->withPath(substr($path, 0, -1));
    
            if($request->getMethod() == 'GET') {
                return $response->withRedirect((string)$uri, 301);
            }
            else {
                return $next($request->withUri($uri), $response);
            }
        }
    
        return $next($request, $response);
    });
    

    (来源:http://www.slimframework.com/docs/cookbook/route-patterns.html

相关问题