【问题标题】:how to use slim redirect如何使用苗条重定向
【发布时间】:2014-06-17 17:36:58
【问题描述】:

我有问题。我正在使用 slim,并且我的主页有路由:

$app->get('/', function() use ($app) { ... 

在我的一个控制器中,我想重定向到主页,所以我写了

$app->response->redirect('/', 303);

但不是重定向到“/”路由,而是重定向到我的本地服务器的根目录,即http://localhost/

我做错了什么?我应该如何使用重定向方法?

【问题讨论】:

    标签: php redirect slim


    【解决方案1】:

    超薄 4:

    $response->withHeader('Location', '/redirect/to');
    

    或者代替固定字符串:

    use Slim\Routing\RouteContext;
    
    $routeParser = RouteContext::fromRequest($request)->getRouteParser();
    $url = $routeParser->urlFor('login');
    
    return $response->withHeader('Location', $url);
    

    Slim 文档:http://www.slimframework.com/docs/v4/objects/response.html#returning-a-redirect 路由上下文:https://discourse.slimframework.com/t/redirect-to-another-route/3582

    【讨论】:

      【解决方案2】:

      修身 3

      $app->get('/', function ($req, $res, $args) {
        $url = 'https://example.org';
        return $res->withRedirect($url);
      });
      

      参考:https://www.slimframework.com/docs/v3/objects/response.html#returning-a-redirect

      【讨论】:

      【解决方案3】:

      我想我遇到了类似的问题,问题出在我的 .htaccess 配置文件上。它实际上应该是这样的:

      RewriteEngine On
      RewriteBase /api #if your web service is inside a subfolder of your app, 
      # you need to pre-append the relative path to that folder, hope this helps you!
      
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*)$ index.php [QSA,L]
      

      【讨论】:

        【解决方案4】:

        对于 Slim v3.x:

        使用$response->withStatus(302)->withHeader('Location', $url); 代替$app->redirect();

        在 Slim v2.x 中,可以使用辅助函数 $app->redirect();触发重定向请求。在 Slim v3.x 中,可以像这样使用 Response 类来做同样的事情(参见下面的例子)[1]。

        使用pathFor() 代替urlFor()

        urlFor() 已重命名为 pathFor() 并且可以在路由器对象中找到。 此外,pathFor() 是基本路径感知[2]。

        例子:

        $app->get('/', function ( $request, $response, $args ) use ( $app ) {
          $url = $this->router->pathFor('loginRoute');
          return $response->withStatus(302)->withHeader('Location', $url);
        });
        

        注意:可以通过传递参数名称和值的关联数组作为pathFor() 的第二个参数来提供附加参数,例如:$this->router->pathFor('viewPost', ['id' => 1]);

        路由器的 pathFor() 方法接受两个参数:

        1. 路线名称
        2. 路由模式占位符和替换值的关联数组[3]

        参考资料:

        1. Changed Redirect
        2. urlFor() is now pathFor() in the router
        3. Route names

        【讨论】:

          【解决方案5】:

          来自 Slim3 文档 http://www.slimframework.com/docs/start/upgrade.html

          $app->get('/', function ($req, $res, $args) {
            return $res->withStatus(302)->withHeader('Location', 'your-new-uri');
          });
          

          【讨论】:

          • 它很完美,并且根据 Slim v3.x 文档也适用于我,谢谢。
          • 你也可以使用$res->withRedirect('your-new-uri');
          • 我在 Slim 4 中收到 withRedirect method not found 错误,所以不得不使用这个答案。
          • @sulaimansudirman 这里也一样!知道为什么 withRedirect 对我们有用吗?
          【解决方案6】:

          Slim 允许您命名路由,然后使用 urlFor() 根据此名称重定向回它们。在您的示例中,将您的路线更改为:

          $app->get('/', function() use ($app) { ... })->name("root");
          

          然后你的重定向变成:

          $app->response->redirect($app->urlFor('root'), 303);
          

          有关详细信息,请参阅 Slim 文档中的 Route Helpers

          【讨论】:

          • 为了避免 URL 劫持,你应该使用 307,因为 HTTP 1.1。
          【解决方案7】:
          //Set Default index or home page
          $app->get('/', function() use ($app) {
                $app->response->redirect('login.php');
          });
          

          【讨论】:

            【解决方案8】:

            给你的“/”路线起个名字,

            $app = new \Slim\Slim();
            $app->get('/', function () {
                   echo "root page";
                })->name('root');
            
            
            $app->get('/foo', function () use ($app) {
                   $app->redirect($app->urlFor('root') );
                });
            
            $app->run();
            

            这应该为您提供正确的重定向网址

            http://docs.slimframework.com/routing/names/ http://docs.slimframework.com/routing/helpers/#redirect

            【讨论】:

              【解决方案9】:

              我认为使用 ./ 代替 / 也可以。

              【讨论】:

                猜你喜欢
                • 2015-12-27
                • 2012-12-04
                • 2021-01-15
                • 2019-01-31
                • 1970-01-01
                • 1970-01-01
                • 2012-04-20
                • 2021-03-30
                • 1970-01-01
                相关资源
                最近更新 更多