How to autopopulate a page based on id

Hello,

I would like to have a page crud with only 1 id showing, I will remove the delete and create pages.

I would like to know what is the best approache to load my "update" page to always record of id = 1

I thought about adding the id to the button link as /create/1

But could this be done inside the controller itself on the "create" action?




    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,

            ]);

        }

    }



Thank you.

If you want remove the id like a parammeter in this controller, only take of it.


public function actionUpdate()

    {

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


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

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

        } else {

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

                'model' => $model,

            ]);

        }

    }

I think it will work.

Thanks, I have already tried this one and it outputs in the view:

Bad Request (#400)

Missing required parameters: id

The above error occurred while the Web server was processing your request.

Please contact us if you think this is a server error. Thank you.

sorry it did not work because I did not remove the $id from the function at the top :slight_smile:

public function actionUpdate()


{


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





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


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


    } else {


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


            'model' => $model,


        ]);


    }


}

All good now :slight_smile:

Thanks again.

Remove the actionCreate() and actionDelete() and don’t change the actionUpdate(). You can personalize you GridView::widget() in your index.php file to remove a delete icon and create buttom.

Without the action anyone will access these methods.


<?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            [

                'class' => 'yii\grid\SerialColumn',

                'headerOptions' => ['width' => '60'],

            ],

            

            'id',

            'field1',

            'field2', 

            'fieldN',

            [

                'class' => 'yii\grid\ActionColumn',

                'headerOptions' => ['class' => 'col-sm-1 text-center'],

                'template' => '{view} {update}',

                'contentOptions' => ['class' => 'text-center'],

            ],

        ],

    ]); ?>

Thanks, Excellent, I was actually looking into it now, I will remove everything one by one in each views,

Thanks again!!! :slight_smile:

I was actually wondering if it is not better to blick the access directly from the sitecontroller using:




 public function behaviors()

    {

        return [

            'access' => [

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

                'rules' => [

                    [

                        'actions' => ['login', 'error'],

                        'allow' => true,

                    ],

                    [

                        'actions' => ['logout', 'index'],

                        'allow' => true,

                        'roles' => ['@'],

                    ],

                ],

            ],

            'verbs' => [

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

                'actions' => [

                    'logout' => ['post'],

                ],

            ],

        ];

    }




What do you think?

Don’t forget to remove the “Creates” and “Deletes” in all your views where you don’t want this behavior.

Yeah, you need remove from here too.

Thanks Calcio:-)