Lucky Numbers
Showing 21-27 of 27 items.
| # | Number | Color | Created At | Updated At | Created By | Updated By | |
|---|---|---|---|---|---|---|---|
| 21 | 567 | #c27291 | 2021-02-11 23:58:59 | 2021-02-11 23:59:10 | alice | alice | |
| 22 | 2 | #5b612b | 2021-02-19 07:27:18 | 2021-02-19 07:27:28 | amy | amy | |
| 23 | 1212 | #c29e4a | 2021-04-25 20:50:12 | 2021-04-25 20:50:12 | amy | amy | |
| 24 | 65535 | #7e3587 | 2021-07-19 12:22:44 | 2021-07-19 12:22:44 | ed | ed | |
| 25 | 33 | #c28836 | 2021-11-09 18:28:56 | 2021-11-09 18:28:56 | amy | amy | |
| 26 | 313 | #53c27a | 2022-02-25 21:20:36 | 2022-02-25 21:20:36 | alice | alice | |
| 27 | 22 | #a800c2 | 2022-04-18 16:38:05 | 2022-04-18 16:38:05 | alice | alice |
Controller code
<?php
use ...
class LuckyNumberController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::class,
'only' => ['view', 'create', 'delete'],
'rules' => [
[
'actions' => ['view'],
'allow' => true,
'roles' => ['@'] // allow all authenticated users
],
[
'actions' => ['create'],
'allow' => true,
'permissions' => ['createItem']
],
[
'actions' => ['delete'],
'allow' => true,
'permissions' => ['deleteItem']
],
]
],
// ... more behaviors ...
];
// ... actionIndex(), actionView() ...
public function actionUpdate($id)
{
$model = $this->findModel($id);
if (! Yii::$app->user->can('updateItem', $model)) {
throw new ForbiddenHttpException('Sorry, you\'re not allowed to update this Lucky Number');
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
// ...
}
}
View code
...
<?php if (Yii::$app->user->can('createItem')): ?>
<p><?= Html::a(Yii::t('app', 'New Lucky Number'), ['create'], ['class' => 'btn btn-outline-success']) ?></p>
<?php endif; ?>
...
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'number',
// ...
'updatedBy.name',
[
'class' => 'yii\grid\ActionColumn',
'visibleButtons' => [
'view' => ! Yii::$app->user->isGuest,
'update' => function ($model, $key, $index) {
return Yii::$app->user->can('updateItem', $model);
},
'delete' => Yii::$app->user->can('deleteItem')
]
],
],
// ...
]); ?>