如何重定向Slim/PHP
我试图在转到/add并执行该功能后重定向,但当我单击按钮时,页面将转到url/add,实现该功能并且不重定向,有人知道我如何执行该操作?除重定向外,所有工作正常如何重定向Slim/PHP,php,slim,Php,Slim,我试图在转到/add并执行该功能后重定向,但当我单击按钮时,页面将转到url/add,实现该功能并且不重定向,有人知道我如何执行该操作?除重定向外,所有工作正常 <?php use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as Response; require '../vendor/autoload.php';
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
$app = new \Slim\App();
$app->get('/domain/{id}', function (Request $request, Response $response, array $args) {
$id = $args['id'];
$response->getBody()->write("Hello, $id");
return $response;
});
$app->get('/domain', function () {
$jsonContents = file_get_contents('data/data.json');
echo $jsonContents;
});
$app->post('/add', function () {
$jsonContents = file_get_contents('data/data.json');
$name = $_POST['addname'];
$url = $_POST['addurl'];
$data = json_decode($jsonContents, true);
$last_item = end($data);
$last_item_id = $last_item['id'];
$data[] = array(
'name' => $name,
'url' => $url,
'id' => $last_item_id+1
);
$json = json_encode($data);
file_put_contents('data/data.json', $json);
header('location:../index.php');
});
$app->run();
解决了,我删除了标题('location:../index.php')我添加了return$this->response->withRedirect('../index.html')代码>尽量不要在位置标头中发送相对url(包括方案-http、主机名和绝对路径),例如http://www.yoursite.com/index.php
),并使用大写字母“L”表示“位置”。