I am creating a yii2 form to add a new product in the index.php (above gridview). I make the call using ajax. The url is set to
...
function submitProduct() {
$.ajax({
type: 'POST',
url: "<?= Yii::$app->getUrlManager()->createUrl('/product/addProduct') ?>",
data: $('#add-product-form').serialize(),
success: formProductSaved,
dataType: 'json'
});
}
...
While inside the ProductController.php
...
public function actionAddproduct() {
if (Yii::$app->request->isPost) {
$model = new Product;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return Json::encode(array(
'status' => 'success',
'title' => 'Add produdct',
'message' => 'Product is added successfully.',
));
} else {
$errors = array_map(function($v) {
return join(', ', $v);
}, $model->getErrors());
return Json::encode(array('status' => 'error', 'title' => 'Add product', 'message' => "Errors: " . implode(" ", $errors)));
}
} else {
throw new HttpException(400, 'Invalid request');
}
}
...
Strangely enough, if I change the function to addproduct, it will work flawlessly. Can someone help to explain about this?
Thank you.
Daniel