I have a function name import in the contact controller. The issue is that when I am trying to make a call to the function with https://example.org/contact/import, the import function is not executing. It shows me 403 error. The other functions of the controller like create is working fine. I am not sure what I am doing wrong here.
Here is my controller code:
<?php
namespace backend\controllers;
use Yii;
use backend\models\Contact;
class ContactController extends Controller
{
protected function newmodel()
{
return new Contact();
}
public function actionCreate()
{
$objModel = $this->newmodel();
$objReflectionClass = new \ReflectionClass($objModel);
$objModel->load([$objReflectionClass->getShortName() => Yii::$app->request->get()]);
$strView = 'create';
if (!file_exists(Yii::getAlias('@backend') . '/views/' . strtolower($objReflectionClass->getShortName()) . '/create.php'))
{
// default
$strView = '/default/create';
}
return $this->render($strView, [
'objModel' => $objModel,
]);
}
public function actionImport()
{
die("In import function");
}
}
Here is the routes rules
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
//'enableStrictParsing' => true,
'rules' => [
'gii'=>'gii/default/login',
'gii/<controller:\w+>'=>'gii/<controller>',
'gii/<controller:\w+>/<action:\w+>'=>'gii/<controller>/<action>',
'debug/<controller>/<action>' => 'debug/<controller>/<action>',
'' => 'site/index',
'forgotpassword' => 'site/forgotpassword',
'<action:(contact|login|logout)>' => 'site/<action>',
'<action:(contact|login|logout)>/*' => 'site/<action>',
'<controller:\w+>/'=>'<controller>/index',
'<controller:\w+>/<intId:\d+>'=>'<controller>/view',
'<controller:\w+>/view/<intId:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<intId:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
],
],
If the create function is working then I believe import function should also work. Can anyone please guide me what I am doing wrong.
Many Thanks!
I tried to manually I add the static route rule for my url but that was not working.
Here is how I added that rule
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
//'enableStrictParsing' => true,
'rules' => [
'gii'=>'gii/default/login',
'gii/<controller:\w+>'=>'gii/<controller>',
'gii/<controller:\w+>/<action:\w+>'=>'gii/<controller>/<action>',
'debug/<controller>/<action>' => 'debug/<controller>/<action>',
'' => 'site/index',
'forgotpassword' => 'site/forgotpassword',
'<action:(contact|login|logout)>' => 'site/<action>',
'<action:(contact|login|logout)>/*' => 'site/<action>',
'<controller:\w+>/'=>'<controller>/index',
'<controller:\w+>/<intId:\d+>'=>'<controller>/view',
'<controller:\w+>/view/<intId:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<intId:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'ContactController/actionImport' => 'contact/import',
],
],
Update
here is the screenshot of what I am getting when open https://example.org/contact/import

https://example.org/contact/importurl'<action:(contact|login|logout)>/*' => 'site/<action>',makes yourcontact/importrequest go tocontactaction insitecontroller instead of yourcontactcontroller. The 403 error might be result of some behavior or the logic inside yoursite/contactaction.