Yii2 POST Parameter Display

I have js script that send data on simular:

$.ajax({ type: "POST", url: '/manage/add-shops/', data: {'id':id, 'shops': shops} 

"stores" is an array with ~ 1000 elements, so I have to send it via POST. I have a yii2 controller with a method:

 class ManageController extends Controller { public function actionAddShops($id, $shops=array()) { .... } 

Routing is fine, but I get this error:

 "Missing required parameters: id" 

It seems that the POST parameters are not mapped to the method parameters. Thanks.

+7
php mapping yii2
source share
5 answers

So, there is no native POST matching, but we can implement it, for example:

 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 the routes:

 ['pattern'=>'manage/<action:\S+>', 'route'=>'manage/<action>', 'suffix'=>'/', 'class' => 'app\components\OurUrlRule',] 
+1
source share

You're right, for some reason, Yii2 only automatically binds GET variables, but unfortunately not POST.

However, you can easily bind to magic; all you have to do is override your controller's runAction() . If you do not want to do this manually for each controller, simply create a base controller and continue it when necessary. Check out the following snippet:

 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 can access the action of your controller $id and $shops as usual, as you did in Yii1.

Hope this helps.

+4
source share

When passing parameters to the controller, these parameters are available only if they are in the GET URL. If you send POST parameters, you will need to access them using the Yii :: $ app-> request-> post method.

so your function will look something like this:

 enter class ManageController extends Controller { public function actionAddShops() { $post = Yii::$app->request->post(); $id = $post['id']; $shops = $post['shops']; } 
-one
source share
 class ManageController extends Controller { public function actionAddShops() { var_dump($_POST); } 

Here you go.

-2
source share
 $.ajax({ type: "POST", url: '/manage/add-shops/', data: {'id':id, 'shops': shops} class ManageController extends Controller { public function actionAddShops($id, $shops=array()) { .... } 

As I understand from your code, you pass the values ​​through ajax using the POST method, but if you use the action method in your controller with parameters, it means that the GET method is required for this action. So just change your ajax method to

 $.ajax({ type: "GET", url: '/manage/add-shops/', data: {'id':id, 'shops': shops} 

And then check that it will work perfectly.

-2
source share

All Articles