php - 当post的请求需要301或者302这么携带post的数据呢
ringa_lee
ringa_lee 2017-04-11 10:09:16
[PHP讨论组]

问题:因为我是已有程序迁移到slim框架上,所以,真对以前的动态地址做了withRedirect,代码如下:

<?php

namespace Ts\Middleware;

use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Interop\Container\ContainerInterface;

/**
 * 兼容的地址,将原有的动态地址转化为伪静态地址
 *
 * @package default
 * @author 
 **/
class CompatibleURL
{
    protected $ci;

    public function __construct(ContainerInterface $ci) {
        $this->ci = $ci;
    }

    /**
     * Compatible URL middleware invokable class.
     *
     * @param  \Psr\Http\Message\RequestInterface  $request  PSR7 request
     * @param  \Psr\Http\Message\ResponseInterface $response PSR7 response
     * @param  callable                            $next     Next middleware
     * @return \Psr\Http\Message\ResponseInterface
     * @author Seven Du <lovevipdsw@outlook.com>
     **/
    public function __invoke(Request $request, Response $response, callable $next)
    {
        $app = $request->getQueryParam('app');
        $mod = $request->getQueryParam('mod');
        $act = $request->getQueryParam('act');

        $param = [];

        if ($app !== null) {
            $param['app'] = $app;
        }

        if ($mod !== null) {
            $param['controller'] = $mod;
        }

        if ($act !== null) {
            $param['action'] = $act;
        }

        $router = $this->ci->get('router');
        $queryParam = $request->getQueryParams();

        unset($queryParam['app'], $queryParam['mod'], $queryParam['act']);

        $uri = $router->pathFor('apps', $param, $queryParam);

        if ($uri != $router->pathFor('apps', [], $queryParam)) {
            // permanently redirect paths with a trailing slash
            // to their non-trailing counterpart
            return $response->withRedirect((string) $uri, 301);
        }

        return $next($request, $response);
    }
} // END class CompatibleURL

而很多一些异步请求为post,301到新的地址后,直接丢失了post的数据,如图:

我记得重定向是可以携带post数据的,但是在slim中,这么从Response设置呢?请教!
感谢?!

ringa_lee
ringa_lee

ringa_lee

全部回复(2)
巴扎黑

没看明白,但是30X代表的应该就是服务器重定向吧,这个状态码是服务器response回来的应该是无疑的。php可以通过header函数来设置。

阿神

首先这些参数应该在301之前处理还是之后处理,好的设计应该是在301之前处理,处理完之后做个重定向,展示相关的结果页面,只带上必要的查询参数,这时不应该再去取POST参数了,而是直接根据查询参数展示结果。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号