Arkham.vm Arkham.vm - 1 year ago 279
PHP Question

yii2 POST parameters mapping

i have some js script that send simular data:

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:

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.

Answer Source

So, there is no native POST mapping, but we can implement it, like this:

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',]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download