【问题标题】:Slim trailing slash route细长的斜线路径
【发布时间】:2018-10-22 09:39:07
【问题描述】:

我在 Slim 应用程序中有这条路线:

$app->get('/fasi/:id/',function ($id) use ($app) {
        $app->render("fasi.html");
});

这回答了

http://test/fasi/1/

还要

http://test/fasi/1

有什么方法可以强制 Slim 只回答带有斜杠(第一个)的 url,或者重定向添加斜杠的客户端?

【问题讨论】:

    标签: php url-routing slim


    【解决方案1】:

    您也可以将斜杠和参数设为可选,如下所示:

    $app->get('/fasi/[:id[/]]',function ($id) use ($app) {
        $app->render("fasi.html");
    });
    

    这适用于:

    http://test/fasi/1

    或:

    http://test/fasi/1/

    【讨论】:

    • 在 Slim 2 中,使用 $app->get('/foo/?',function () use ($app) 确实有效,而 $app->get('/foo[/]',function () use ($app) 无效
    • 我想通过重定向到不带斜线的域来实现
    【解决方案2】:

    如果您想将所有以 / 结尾的 URL 重定向/重写为非尾随 / 等价物,则可以添加此中间件:

     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);
        });
    

    【讨论】:

      【解决方案3】:

      您可以将斜杠设为可选

      $app->get('/fasi/:id(/)',function ($id) use ($app) {
          $app->render("fasi.html");
      });
      

      或者添加一个重定向到带有斜杠的路由的路由

      $app->get('/fasi/:id/',function ($id) use ($app) {
          $app->urlFor('fasi', array('id' => $id));
      });
      
      $app->get('/fasi/:id',function ($id) use ($app) {
          $app->urlFor('hello', array('name' => 'Josh'));
      })->name('fasi');
      

      或者让 Apache 将您的请求重定向到相同的 url + 斜杠,请记住,这将重定向所有 url 以添加斜杠

      RewriteEngine On
      RewriteBase /
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_URI} !(.*)/$
      RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
      

      【讨论】:

      • 第一个答案不是我需要的:默认情况下,尾部斜杠似乎已经是可选的。第三个有点太多(但可以是一个解决方案!)。我觉得第二个很好,我会试试的。
      • 还没有,但我会尽快尝试,我会记得标记您的答案。谢谢
      • v3 中的语法是 /fasi/:id[/] 而不是 /fasi/:id(/) slimframework.com/docs/objects/router.html#route-placeholders
      【解决方案4】:
      $app->get('/fasi/{id[0-9]*}{slash:[/]?}',function ($id) use ($app) {
          $app->render("fasi.html");
      }
      

      试试这个。

      【讨论】:

      • 欢迎来到 Stack Overflow!感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation would greatly improve its long-term value 通过展示为什么这是一个很好的解决问题的方法,并将使其对未来有其他类似问题的读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
      • @Satit 请提供一些解释。
      【解决方案5】:

      这就是我对基于超薄框架的应用所做的

      //updated code, we do not want .js .css .png to have trailing slash
          $app->add(function (Request $request, Response $response, callable $next) {
          $uri = $request->getUri();
          $files = array('.js','.css','.jpeg','.png','.jpg');
          $path = $uri->getPath();
          foreach ($files as $file) {
              if (strpos($uri, $file) == TRUE) {
                  return $next($request, $response);          
              }
              else {
                  if ($path != '/' && substr($path, -1) != '/') {
                  $uri = $uri.'/';
                  return $response->withRedirect((string)$uri, 301);
                  }
              }
          }
          return $next($request, $response);
      });
      

      【讨论】:

      • if ($path != '/' && substr($path, -1) != '/')-loop 中的if ($path != '/' && substr($path, -1) != '/')-条件没有意义,因为它与$file 无关。今天,我们这样做:github.com/middlewares/trailing-slash :)
      猜你喜欢
      • 2021-02-02
      • 2011-09-19
      • 2017-11-02
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 2013-08-01
      • 1970-01-01
      相关资源
      最近更新 更多