action parameters

Hello guys,

I am pretty new to Yii2, about a week or so.

Simply put. How can I pass action parameters?

My current controller looks like this:




    public function actionEdit($id)

    {

        echo 'ID='.$id;die;

    }



I try to access it through localhost/users/manager/edit/id/1 . I get 404 error.

I have prettyUrl setup in place.

I appreciate your assistance on this.

Thanks,

Check this.

Thanks! Here what I have done after reading provided link.

As a note, I am using advanced app with modules (User module).

  • Components urlManager section of backend/config/main.php



        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

            'enableStrictParsing' => false,

            'rules' => [

                '<controller:(manager)>/<action:(edit|delete)>/<id:\d+>' => '<controller>/<action>',

            ]

        ],



  • UserController:



class ManagerController extends Controller

{

    public function behaviors()

    {

        return [

            'access' => [

                'class' => AccessControl::className(),

                'rules' => [

                    [

                        'actions' => ['ajax', 'index', 'new', 'edit'],

                        'allow' => true

                    ]

                ]

            ],

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'ajax' => ['post']

                ]

            ]

        ];

    }

    public function actionEdit($id)

    {

        echo 'ID='.$id;die;

    }



  • Attempted to access the backend URL as follows and I still 404 error

backend.localhost/user/manager/edit/1

  • user: Is backend module name.

  • manager: Is desired controller.

  • edit: requested action.

Please correct me if I am missing anything here.

Thanks,

Sorry, that was a bad example. Just use the standard rule with <id:\d+>.




'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',

'<controller:\w+>/<action:\w+>' => <controller>/<action>',



I don’t know if the use of a module will make a difference. (I’m still learning Yii2.)

Can you access other actions at backend.localhost/user/manager?

Hi Tri,

I used your rules. It is still 404.

Other actions are working just fine, its just passing params to action doesn’t work.

Any other rules that may interfere?

What does the log say. If you comment out "level" in config you will get verbose logging. Or just add the level "trace".

Call to myapp/vehicle/1


2016-12-16 11:33:13 [192.168.4.11][-][-][trace][yii\web\UrlRule::parseRequest] Request parsed with URL rule: <controller:\w+>/<id:\d+>

2016-12-16 11:33:13 [192.168.4.11][-][-][trace][yii\web\Application::handleRequest] Route requested: 'vehicle/view'

2016-12-16 11:33:13 [192.168.4.11][-][-][trace][yii\base\Controller::runAction] Route to run: vehicle/view



Call to myapp/vehicle/view/1


2016-12-16 11:46:07 [192.168.4.11][-][-][trace][yii\web\UrlRule::parseRequest] Request parsed with URL rule: <controller:\w+>/<action:\w+>/<id:\d+>

2016-12-16 11:46:07 [192.168.4.11][-][-][trace][yii\web\Application::handleRequest] Route requested: 'vehicle/view'

2016-12-16 11:46:07 [192.168.4.11][-][-][trace][yii\base\Controller::runAction] Route to run: vehicle/view



and a call to myapp/gii/model


2016-12-16 11:37:02 [192.168.4.11][-][-][trace][yii\web\UrlRule::parseRequest] Request parsed with URL rule: gii/<id:\w+>

2016-12-16 11:37:02 [192.168.4.11][-][-][trace][yii\web\Application::handleRequest] Route requested: 'gii/default/view'

2016-12-16 11:37:02 [192.168.4.11][-][-][trace][yii\base\Controller::runAction] Route to run: gii/default/view



Note: the latter uses default route and \w+ parameter.

I replaced all previous rules with




'<module:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>',



Now I got missing argument error




Missing argument 1 for backend\modules\Users\controllers\ManagerController::actionEdit()



Url: backend.localhost/user/manager/edit/1

Error Log:




2016-12-16 12:54:30 [127.0.0.1][-][-][trace][yii\base\Application::bootstrap] Bootstrap with yii\log\Dispatcher

2016-12-16 12:54:30 [127.0.0.1][-][-][trace][yii\base\Module::getModule] Loading module: gii

2016-12-16 12:54:30 [127.0.0.1][-][-][trace][yii\base\Application::bootstrap] Bootstrap with yii\gii\Module::bootstrap()

2016-12-16 12:54:30 [127.0.0.1][-][-][trace][yii\web\UrlRule::parseRequest] Request parsed with URL rule: <module:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>

2016-12-16 12:54:30 [127.0.0.1][-][-][trace][yii\web\Application::handleRequest] Route requested: 'directory/country/edit'

2016-12-16 12:54:30 [127.0.0.1][-][-][trace][yii\base\Module::getModule] Loading module: user

2016-12-16 12:54:30 [127.0.0.1][-][-][trace][yii\base\Controller::runAction] Route to run: user/manager/edit

2016-12-16 12:54:30 [127.0.0.1][-][-][trace][yii\base\InlineAction::runWithParams] Running action: backend\modules\Users\controllers\ManagerController::actionEdit()

2016-12-16 12:54:30 [127.0.0.1][-][-][error][yii\base\ErrorException:2] yii\base\ErrorException: Missing argument 1 for backend\modules\Users\controllers\ManagerController::actionEdit() in D:\www\htdocs\yii\backend\modules\Users\controllers\ManagerController.php:59



It’s working now.

I just extend ManagerController from yii\web\Controller instead of yii\base\Controller.