php yii2 POST 参数映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28209465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
yii2 POST parameters mapping
提问by arkhamvm
i have some js script that send simular data:
我有一些发送模拟数据的js脚本:
$.ajax({
type: "POST",
url: '/manage/add-shops/',
data: {'id':id, 'shops': shops}
'shops' is array with ~1000 elements, so i should send it via POST. I have a yii2 Controller with method:
'shops' 是包含 ~1000 个元素的数组,所以我应该通过 POST 发送它。我有一个带有方法的 yii2 控制器:
class ManageController extends Controller {
public function actionAddShops($id, $shops=array()) {
....
}
Routing is Ok, but i get this error:
路由没问题,但我收到此错误:
"Missing required parameters: id"
It's look like that POST params doesn't mapped to method params. Thanks.
看起来 POST 参数没有映射到方法参数。谢谢。
采纳答案by arkhamvm
So, there is no native POST mapping, but we can implement it, like this:
所以,没有原生的 POST 映射,但我们可以实现它,像这样:
class OurUrlRule extends UrlRule implements UrlRuleInterface {
public function parseRequest($manager, $request, $add_post = true, $add_files = true) {
$result = parent::parseRequest($manager, $request);
if($result !== false) {
list($route, $params) = $result;
if($add_post === true) {
$params = array_merge($params,$_POST);
}
if($add_files === true) {
$params = array_merge($params,$_FILES);
}
return [$route, $params];
}
return false;
}
}
And then, add to routes:
然后,添加到路由:
['pattern'=>'manage/<action:\S+>', 'route'=>'manage/<action>', 'suffix'=>'/', 'class' => 'app\components\OurUrlRule',]
回答by smich
You are right, for some reason, Yii2 only automatically binds GET variables, but unfortunately not POST.
你是对的,出于某种原因,Yii2 只会自动绑定 GET 变量,但不幸的是不会绑定 POST。
However, you can easily do the magic binding; all you have to do is to override the runAction()
of your controller. If you don't want to do it manually for every controller, just create a base controller and extend from it when required. Check the following snippet:
但是,您可以轻松地进行魔术绑定;您所要做的就是覆盖runAction()
控制器的 。如果您不想为每个控制器手动执行此操作,只需创建一个基本控制器并在需要时从它扩展。检查以下代码段:
public function runAction($id, $params = [])
{
// Extract the params from the request and bind them to params
$params = \yii\helpers\BaseArrayHelper::merge(Yii::$app->getRequest()->getBodyParams(), $params);
return parent::runAction($id, $params);
}
Then you will be able to access in your controller action $id
and $shops
normally as you used to do in Yii1.
然后您将能够在您的控制器操作中访问,$id
并且$shops
通常就像您在 Yii1 中所做的那样。
Hope this helps.
希望这可以帮助。
回答by Joe Miller
When you pass parameters to a controller action, those parameters are only available if they are in the GET of a url. If you are sending parameters by POST, then you will need to access them via the Yii::$app->request->post method.
当您将参数传递给控制器操作时,这些参数仅当它们位于 url 的 GET 中时才可用。如果您通过 POST 发送参数,那么您需要通过 Yii::$app->request->post 方法访问它们。
so your function ends up looking something like this;
所以你的函数最终看起来像这样;
enter class ManageController extends Controller {
public function actionAddShops() {
$post = Yii::$app->request->post();
$id = $post['id'];
$shops = $post['shops'];
}
回答by Neeraj Kumar
$.ajax({
type: "POST",
url: '/manage/add-shops/',
data: {'id':id, 'shops': shops}
class ManageController extends Controller {
public function actionAddShops($id, $shops=array()) {
....
}
What i have understand from your code is you are passing values through ajax using POST method but if your are using an action method in your controller with parameters then it means this action needs GET method for use. So simply change your ajax method to
我从您的代码中了解到您正在使用 POST 方法通过 ajax 传递值,但是如果您在控制器中使用带有参数的操作方法,那么这意味着该操作需要使用 GET 方法。所以只需将您的 ajax 方法更改为
$.ajax({
type: "GET",
url: '/manage/add-shops/',
data: {'id':id, 'shops': shops}
And then check it will work perfectly.
然后检查它是否可以正常工作。
回答by Vick
class ManageController extends Controller {
public function actionAddShops() {
var_dump($_POST);
}
Here you go.
干得好。