首頁  >  文章  >  後端開發  >  當post的請求需要301或302這麼攜帶post的資料呢

當post的請求需要301或302這麼攜帶post的資料呢

WBOY
WBOY原創
2016-08-31 08:54:521983瀏覽

問題:因為我是已有程式遷移到slim框架上,所以,真對以前的動態地址做了withRedirect,程式碼如下:

<code class="php"><?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
</code>

而很多一些非同步請求為post,301到新的位址後,直接遺失了post的數據,如圖:
當post的請求需要301或302這麼攜帶post的資料呢

當post的請求需要301或302這麼攜帶post的資料呢

我記得重定向是可以攜帶post資料的,但是在slim中,這麼從Response設定呢?請教!
感謝?!

回覆內容:

問題:因為我是已有程式遷移到slim框架上,所以,真對以前的動態地址做了withRedirect,程式碼如下:

<code class="php"><?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
</code>

而很多一些非同步請求為post,301到新的位址後,直接遺失了post的數據,如圖:
當post的請求需要301或302這麼攜帶post的資料呢

當post的請求需要301或302這麼攜帶post的資料呢

我記得重定向是可以攜帶post資料的,但是在slim中,這麼從Response設定呢?請教!
感謝?!

沒看明白,但是30X代表的應該就是伺服器重定向吧,這個狀態碼是伺服器response回來的應該是無疑的。 php可以透過header函數來設定。

首先這些參數應該在301之前處理還是之後處理,好的設計應該是在301之前處理,處理完之後做個重定向,展示相關的結果頁面,只帶上必要的查詢參數,這時不應該再去取POST參數了,而是直接根據查詢參數展示結果。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn