When a post request requires 301 or 302 to carry the post data?

WBOY
Release: 2016-08-31 08:54:52
Original
2009 people have browsed it

Question: Because I have already migrated the program to the slim framework, I really did withRedirect on the previous dynamic address. The code is as follows:

<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>
Copy after login
Copy after login

And many asynchronous requests are post. After 301 is sent to the new address, the post data is directly lost, as shown in the picture:
When a post request requires 301 or 302 to carry the post data?

When a post request requires 301 or 302 to carry the post data?

I remember that redirection can carry post data, but in slim, what about setting it from Response? consult!
Thank you?!

Reply content:

Question: Because I have migrated an existing program to the slim framework, I really did withRedirect on the previous dynamic address. The code is as follows:

<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>
Copy after login
Copy after login

And many asynchronous requests are post. After 301 is sent to the new address, the post data is directly lost, as shown in the picture:
When a post request requires 301 or 302 to carry the post data?

When a post request requires 301 or 302 to carry the post data?

I remember that redirection can carry post data, but in slim, what about setting it from Response? consult!
Thank you?!

I don’t understand clearly, but 30X should represent server redirection. There should be no doubt that this status code comes from the server response. PHP can be set through the header function.

First of all, should these parameters be processed before 301 or after? A good design should be to process before 301. After processing, make a redirect and display the relevant results page, with only the necessary query parameters. At this time, there should be no more Instead of getting the POST parameters, the results are displayed directly based on the query parameters.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!