如何重定向 Slim/PHP
How can I redirect Slim/PHP
我试图在转到 /add 并执行该功能后重定向,但是当我单击按钮时,页面转到 url/add,实现该功能而不重定向,有人知道如何我可以做吗?除重定向外的所有作品。
<?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();
解决了,我删了header('location:../index.php');
我加了return $this->response->withRedirect('../index.html');
我试图在转到 /add 并执行该功能后重定向,但是当我单击按钮时,页面转到 url/add,实现该功能而不重定向,有人知道如何我可以做吗?除重定向外的所有作品。
<?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();
解决了,我删了header('location:../index.php');
我加了return $this->response->withRedirect('../index.html');