How Can I Call A Action With Parameters?

When I use gii to create CRUD behaviour the update form and action are working out of the box. I don’t know how it exactly works. The action of the form is automatically generated and actionUpdate($id) gets the right id.

Now I’d like to add another form in the update view. This form should call another action (same controller) and then forward back to to the update view. My questions:

  • How do I define the target action?

  • And how do I pass the model id so actionDoSomething($id) will get this id as argument? Or does this happen automatically with defining the action?

  • Is it possible to declare more action parameters? Or is only one supported?

I’m confused how views and controllers work together.

Robert

Hey,

I believe the default rule set is:


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

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

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



That means if you do in your controller a custom action


class MyController {

    public function actionSomethingElse($id) {

    }

}

if you want to have multiple parameters you have to create a custom rule in the url manager like:


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

then your class would look like:


class MyController {

    public function actionSomethingElse($customId, $customName) {

    }

}

Thank you for your input.

Actually, my question wasn’t about a gii itself. I don’t want to change its behaviour. I just want to create an action in my existing controller and view.

For example:




class LanguageController {


    //Generated action, unmodified

    public function actionUpdate($id)

    {

        $model = $this->findModel($id);


        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->id]);

        } else {

            return $this->render('update', [

                'model' => $model,

            ]);

        }

    }


    //My new action

    public function actionSomethingElse($id) {

        //do something

        return $this->redirect(['update', 'id' => $id]);

    }

}



and in the update.php view I have added the ‘My own form’ section and didn’t change anything else:




use yii\helpers\Html;

use yii\widgets\ActiveForm;


/* @var $this yii\web\View */

/* @var $model app\models\core\Language */


$this->title = Yii::t('app', 'Update {modelClass}: ', ['modelClass' => 'Language']) . ' ' . $model->name;

$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Languages'), 'url' => ['index']];

$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]];

$this->params['breadcrumbs'][] = Yii::t('app', 'Update');

?>


<div class="language-update">


    <h1><?= Html::encode($this->title) ?></h1>


    <?= $this->render('_form', [

        'model' => $model,

    ]) ?>


    <!-- My own form -->

    <?php $form = ActiveForm::begin([

        //'action' => 'something-else', <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />??

    ]); ?>

    

        <?= $form->field($model, 'someText')->textInput(); ?>

        

        <?= Html::submitButton(Yii::t('app', 'Submit'), ['class' => 'btn btn-primary']) ?>

    

    <?php ActiveForm::end(); ?>

    <!-- My own form -->

</div>



I’m not sure. Do I have to specify the action in ActiveForm::begin()? And how will the model id get passed to the action method? I’m dealing here with the same model that is used in _form.php.

Btw: I know it is a form on its own - all data of the other form (in _form.php) won’t be submitted. This could be a problem. However, first of all I just want to know how to add a second form.

Now I have modified the form beginning. I think that could work. Though I don’t know if this is right way.




<?php $form = ActiveForm::begin([

    'action' => \yii\helpers\Url::to(['language/something-else', 'id' => $model->id]),

]); ?>



I think this is the right way.

Thanks! It seems so. At least it works for me so far.