K. Weber
K. Weber

Reputation: 2773

Yii2: GridView: add button or link to controller action

I have a controller with an action method:

namespace frontend\controllers;

class EmployeeController extends FrontController
{

    /**
     * Deletes an existing Employee status.
     * @param integer $id
     * @return mixed
     */
    public function actionDeleteStatus($status_id)
    {
        error_log("actionDeleteStatus " . $status_id);

        return $this->redirect(['update']);
    }


}

In update form, I have a detail GridView, in which I want to add a "delete" link with an URL for this method as a GET request.

I try to get the URL with this: Url::toRoute(['employee/deleteStatus','status_id' => $model->status_id]) which gives me an url like /employee/deleteStatus?status_id=4 and throws a 404, here is the detailed code:

<div class="col-xs-12">
    <?php
        echo Html::label(Yii::t('app', 'Employee status history'));
        echo GridView::widget([
            'summary' => '',
            'options' => [
                'id' => 'status-history',
            ],
            'emptyText' => '',
            'export' => false,
            'dataProvider' => $statusHistory,
            'columns' => [
                [...],
                [
                    'class'     => 'kartik\grid\DataColumn',
                    'attribute' => 'status_id',
                    'headerOptions'  => [ 'class' => 'kv-grid-hide' ],
                    'contentOptions' => [ 'class' => 'kv-grid-hide' ]
                ],
                [
                    'class' => 'yii\grid\ActionColumn',
                    'urlCreator' => function($action, $model, $key, $index) {
                                                return Url::toRoute(['employee/deleteStatus','status_id' => $model->status_id]);
                                        },
                    'template' => '{delete}',
                    'contentOptions' => ['class' => 'column-action'],
                    'buttons'           => [
                        'delete' => function ($url, $model, $key) {
                            if (Yii::$app->user->can('globalDAF')) {
                                $options = [
                                    'title' => Yii::t('app', 'Delete'),
                                    'aria-label' => Yii::t('app', 'Delete'),
                                    'data-confirm' => Yii::t('app', 'Sure to delete status?'),
                                    'data-method' => 'post',
                                    'data-pjax' => '0',
                                    'class' => 'btn-llyc'
                                ];
                                return Html::a('<span class="glyphicon glyphicon-remove"></span>', $url, $options);
                            } else {
                                return;
                            }
                        }
                    ]
                ]
            ],
            'hover' => true,
            'responsiveWrap' => false
        ]);
    ?>
</div>

Is the url generation wrong? Why am I getting a 404?

Thanks.

Upvotes: 1

Views: 1280

Answers (1)

user206
user206

Reputation: 1105

For example, index becomes actionIndex, and hello-world becomes actionHelloWorld.

Note: The names of the action methods are case-sensitive. If you have a method named ActionIndex, it will not be considered as an action method, and as a result, the request for the index action will result in an exception. Also note that action methods must be public. A private or protected method does NOT define an inline action. Link

Url::toRoute(['employee/delete-status','status_id' => $model->status_id])

Or in config file:

    'urlManager' => [
        'class' => 'yii\web\UrlManager',
         #code ..

        'rules' => [
            'employee/deleteStatus' => 'employee/delete-status',
        ],
    ],

Upvotes: 1

Related Questions