Slim 框架 3:如何从 get 路由中的 post 路由的重定向中获取参数
Slim framework 3: how to get parameters from a redirect from a post route inside a get route
这似乎是一个简单的例子,但我被卡住了。我正在观看一个教程,其中我们看到一个联系表单,它有自己的获取路由来呈现包含已经说过的联系表单的视图。
$app->get('/contact',function (Request $request, Response $response){
return $this->view->render($response,'contact.twig');
})->setName('contact');
然后我们有一个 post 通过该表单获取 posted 数据(注意我正在传递从表单收集的数据)。
$app->post('/contact',function ($request,$response){
$data = $request->getParams();
//var_dump($data);
return $response->withRedirect($this->router->pathFor('contact.confirmed', ['data' => $data]));//https://github.com/slimphp/Slim/issues/1933
})->setName('contact');
最后,我们有另一个 get 路由来呈现确认视图,让用户知道信息已成功提交。
$app->get('/contact/confirmed',function(Request $request, Response $response, $data){
echo 'params are';
var_dump($data);//They never show! :(
return $this->view->render($response,'contact_confirm.twig',[
'data' => $request->getParams(),
]);//https://github.com/slimphp/Slim/issues/1579
})->setName('contact.confirmed');
在该确认视图中,我想检索在 post 路由中提交的数据,只是为了通过他们的名字调用用户,但我收到一条错误消息,指出 $data
为空。
我一直在为如何检索用户名而苦恼。
作为一种变通方法,我通过直接从 post 路线渲染视图解决了这个问题...
$app->post('/contact',function ($request,$response){
$data = $request->getParams();
//var_dump($data);
return $response->withRedirect($this->router->pathFor('contact.confirmed', ['data' => $data]));//https://github.com/slimphp/Slim/issues/1933
})->setName('contact');
但后来我想知道为什么要费心使用 withRedirect()
功能?
我的问题是,如何将数据或参数从 post 路由传递到使用 withRedirect()
函数的 get 路由?在 get 路由中,你如何检索这些参数?所以我可以将它们传递给相应的视图。
已解决
感谢 jmattheis 我是这样解决的:
我刚刚学习了如何在 slim framework 3 中使用控制器,所以 ContactController.php
看起来像:
<?php
namespace App\Controllers\contact;
use App\Controllers\Controller;
class ContactController extends Controller
{
public function show($request,$response)
{
return $this->c->view->render($response,'contact.twig');
}
public function store($request,$response)
{
echo 'storing comments in db ...';
$data = $request->getParams();
//var_dump($data);
//echo 'Name is: '.$data['name'];
$this->c->flash->addMessage('data', $data);
return $response->withRedirect($this->c->router->pathFor('contact.success'));
}
public function success($request,$response,$data)
{
//echo 'Data to be sent in the redirect: ';
$data = $this->c->flash->getFirstMessage('data');
//var_dump($data);
//die();
return $this->c->view->render($response,'contact_success.twig',compact('data'));
}
}
并且我添加了以下代码
session_start();
ini_set('date.timezone', 'America/Mexico_City');
就在
之前
$app = new App([
'settings' => [
'displayErrorDetails' => true
]
]);
成功了。
您不能通过重定向传输数据。路由器上 pathFor
方法的第二个参数是命名参数的数组。那将是 f.ex。 id
在路线 /user/{id}
中然后你必须把 ['id' => 1]
放在里面。
如果你$data
有一个简单的结构,那么你可以把它们放在第三个参数中,这是查询参数,然后用$request->getQueryParams()
读出它们
$this->router->pathFor('contact.confirmed', [], $data);
或者,您可以将数据放在会话变量中,slimphp/Slim-Flash 是一个库。
示例:
$app->post('/contact',function ($request,$response){
$data = $request->getParams();
$this->flash->addMessage('data', $data);
return $response->withRedirect($this->router->pathFor('contact.confirmed'));
})->setName('contact');
$app->get('/contact/confirmed',function(Request $request, Response $response, $data){
$data = $this->flash->getFirstMessage('data');
// ...
});
假设确认模板仅在处理表单后呈现,则无需为此类视图声明单独的 GET 路由。
因此,保留呈现表单的路由:
// This route renders the contact form.
$app->get('/contact', function ($request, $response) {
return $this->view->render($response, 'contact.twig');
})->setName('contact');
并且仅作为发布表单的结果呈现 contact_confirm.twig
:
// This route is contact form processor.
$app->post('/contact', function ($request, $response) {
// Get submitted data.
$formData = $request->getParams();
// Process form data, e.g. store it, sending it somewhere...
// Render confirmation template, passing user name as template parameter.
$templateParams = [
'userName' => $formData['userName'] ?? 'Name unknown';
];
return $this->view->render($response, 'contact_confirmed.twig', $templateParams);
});
这似乎是一个简单的例子,但我被卡住了。我正在观看一个教程,其中我们看到一个联系表单,它有自己的获取路由来呈现包含已经说过的联系表单的视图。
$app->get('/contact',function (Request $request, Response $response){
return $this->view->render($response,'contact.twig');
})->setName('contact');
然后我们有一个 post 通过该表单获取 posted 数据(注意我正在传递从表单收集的数据)。
$app->post('/contact',function ($request,$response){
$data = $request->getParams();
//var_dump($data);
return $response->withRedirect($this->router->pathFor('contact.confirmed', ['data' => $data]));//https://github.com/slimphp/Slim/issues/1933
})->setName('contact');
最后,我们有另一个 get 路由来呈现确认视图,让用户知道信息已成功提交。
$app->get('/contact/confirmed',function(Request $request, Response $response, $data){
echo 'params are';
var_dump($data);//They never show! :(
return $this->view->render($response,'contact_confirm.twig',[
'data' => $request->getParams(),
]);//https://github.com/slimphp/Slim/issues/1579
})->setName('contact.confirmed');
在该确认视图中,我想检索在 post 路由中提交的数据,只是为了通过他们的名字调用用户,但我收到一条错误消息,指出 $data
为空。
我一直在为如何检索用户名而苦恼。
作为一种变通方法,我通过直接从 post 路线渲染视图解决了这个问题...
$app->post('/contact',function ($request,$response){
$data = $request->getParams();
//var_dump($data);
return $response->withRedirect($this->router->pathFor('contact.confirmed', ['data' => $data]));//https://github.com/slimphp/Slim/issues/1933
})->setName('contact');
但后来我想知道为什么要费心使用 withRedirect()
功能?
我的问题是,如何将数据或参数从 post 路由传递到使用 withRedirect()
函数的 get 路由?在 get 路由中,你如何检索这些参数?所以我可以将它们传递给相应的视图。
已解决
感谢 jmattheis 我是这样解决的:
我刚刚学习了如何在 slim framework 3 中使用控制器,所以 ContactController.php
看起来像:
<?php
namespace App\Controllers\contact;
use App\Controllers\Controller;
class ContactController extends Controller
{
public function show($request,$response)
{
return $this->c->view->render($response,'contact.twig');
}
public function store($request,$response)
{
echo 'storing comments in db ...';
$data = $request->getParams();
//var_dump($data);
//echo 'Name is: '.$data['name'];
$this->c->flash->addMessage('data', $data);
return $response->withRedirect($this->c->router->pathFor('contact.success'));
}
public function success($request,$response,$data)
{
//echo 'Data to be sent in the redirect: ';
$data = $this->c->flash->getFirstMessage('data');
//var_dump($data);
//die();
return $this->c->view->render($response,'contact_success.twig',compact('data'));
}
}
并且我添加了以下代码
session_start();
ini_set('date.timezone', 'America/Mexico_City');
就在
之前$app = new App([
'settings' => [
'displayErrorDetails' => true
]
]);
成功了。
您不能通过重定向传输数据。路由器上 pathFor
方法的第二个参数是命名参数的数组。那将是 f.ex。 id
在路线 /user/{id}
中然后你必须把 ['id' => 1]
放在里面。
如果你$data
有一个简单的结构,那么你可以把它们放在第三个参数中,这是查询参数,然后用$request->getQueryParams()
$this->router->pathFor('contact.confirmed', [], $data);
或者,您可以将数据放在会话变量中,slimphp/Slim-Flash 是一个库。
示例:
$app->post('/contact',function ($request,$response){
$data = $request->getParams();
$this->flash->addMessage('data', $data);
return $response->withRedirect($this->router->pathFor('contact.confirmed'));
})->setName('contact');
$app->get('/contact/confirmed',function(Request $request, Response $response, $data){
$data = $this->flash->getFirstMessage('data');
// ...
});
假设确认模板仅在处理表单后呈现,则无需为此类视图声明单独的 GET 路由。
因此,保留呈现表单的路由:
// This route renders the contact form.
$app->get('/contact', function ($request, $response) {
return $this->view->render($response, 'contact.twig');
})->setName('contact');
并且仅作为发布表单的结果呈现 contact_confirm.twig
:
// This route is contact form processor.
$app->post('/contact', function ($request, $response) {
// Get submitted data.
$formData = $request->getParams();
// Process form data, e.g. store it, sending it somewhere...
// Render confirmation template, passing user name as template parameter.
$templateParams = [
'userName' => $formData['userName'] ?? 'Name unknown';
];
return $this->view->render($response, 'contact_confirmed.twig', $templateParams);
});