0

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

8
  • Could you tell me what response you have when running contact/import ? When you declare a route, the left argument (key) is the pattern your user use to access your function, the right being where is your function so your static route is incorrect. Commented Dec 19, 2024 at 13:13
  • @SamHuffman Thank you for looking at it. I updated my question above with a screenshot of 404 page which I am getting when I open https://example.org/contact/import url Commented Dec 20, 2024 at 10:00
  • The screenshot you gave mentions a 403 not a 404. Commented Dec 21, 2024 at 13:02
  • My bad. Updated error code in my question above. Commented Dec 23, 2024 at 2:16
  • This rule '<action:(contact|login|logout)>/*' => 'site/<action>', makes your contact/import request go to contact action in site controller instead of your contact controller. The 403 error might be result of some behavior or the logic inside your site/contact action. Commented Dec 23, 2024 at 10:49

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.