Yii 2.0: Editing, Deleting, and Viewing model data with the yii2-detail-view extension


The yii2-detail-view extension offers you an easy way to EDIT and VIEW your model data, toggle these modes, and adds other features. You can read the extension documentation and/or view a demo of this extension, to understand usage.

This wiki explains additional settings that should be done to your controller to work with the EDIT mode and triggering actions on the SAVE and DELETE buttons.

Note: To use the yii2-datecontrol extension with this widget, you can refer this wiki.


Let's say you have a database table named tbl_book with these columns:

id INT(11)
book_code VARCHAR(30)
synopsis TEXT
color VARCHAR(10)
publish_date DATE
status TINYINT(1)
sale_amount DECIMAL(11,2)
buy_amount DECIMAL(11,2)

Generate the Book model and CRUD through Gii.


After generating the CRUD, delete the actionUpdate method in your BookController.php. Incorporate these changes below:


Change the actionView method to work with the yii2-detail-view extension.

class BookController extends Controller
    public function actionView($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('view', ['model' => $model]);


You can retain your actionDelete as generated by CRUD, or modify it for your needs.

class BookController extends Controller
    public function actionDelete($id) {
        return $this->redirect(['index']);


Edit your view file view.php to work with the yii2-detail-view extension.

use kartik\detail\DetailView;
$this->title = 'View Book'; // $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Books', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
// setup your attributes
$attributes = [
        'value'=>"<span class='badge' style='background-color: {$model->color}'> </span> " . 
        'value'=>$model->status ? 
            '<span class="label label-success">Yes</span>' : 
            '<span class="label label-danger">No</span>',
        'label'=>'Sale Amount ($)',
        'value'=>Html::a('John Steinbeck', '#', ['class'=>'kv-author-link']),
            'data'=>ArrayHelper::map(Author::find()->orderBy('name')->asArray()->all(), 'id', 'name'),
            'options'=>['placeholder'=>'Select ...'],
        'value'=>'<span class="text-justify"><em>' . $model->synopsis . '</em></span>',
echo DetailView::widget([
        'url'=>['delete', 'id' => $model->id],
            'confirm'=>Yii::t('app', 'Are you sure you want to delete this record?'),

That's it, now when you browse to localhost/book/view?id=<id> you should be able to update, save, and delete your records.

Wiki Translations

Total 2 comments

#20165 report it
shiv at 2018/02/06 06:06am
Very nice

Good extention

#19237 report it
lujisheng at 2015/04/25 10:42am
ERROR: Class 'Author' not found

Thank you for your work! There is a error notice:Class 'Author' not found when i run it.

Leave a comment

Please to leave your comment.

Write new article