Developer tester
Developer tester

Reputation: 11

My URL is not calling the function of the controller Yii2

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

enter image description here

Upvotes: 0

Views: 51

Answers (1)

The 403 forbidden error is because you have no 'access' behaviour defined. Try adding this method:

public function behaviors()
{
    return [
        'access' => [
            'class' => \yii\filters\AccessControl::className(),
            'rules' => [
                [
                    'actions'=>['create','import'],
                    'allow' => true,
                ],
                // everything else is denied
            ],
        ],
    ];
}

See The Definitive Guide to Yii 2.0

Upvotes: 0

Related Questions