1. Model (Post.php)
Buat model Post
di models/Post.php
.
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class Post extends ActiveRecord
{
public static function tableName()
{
return 'post';
}
public function rules()
{
return [
[['title', 'content'], 'required'],
[['content'], 'string'],
[['title'], 'string', 'max' => 255],
];
}
}
2. Kontroler (PostController.php)
Buat kontroler PostController
di controllers/PostController.php
.
namespace app\controllers;
use Yii;
use app\models\Post;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
class PostController extends Controller
{
public function actionIndex()
{
$posts = Post::find()->all();
return $this->render('index', ['posts' => $posts]);
}
public function actionCreate()
{
$model = new Post();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
}
return $this->render('create', ['model' => $model]);
}
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
}
return $this->render('update', ['model' => $model]);
}
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
protected function findModel($id)
{
if (($model = Post::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
3. Tampilan
Buat tampilan untuk index
, create
, dan update
.
Related Article
index.php
Buat file views/post/index.php
.
<?php
use yii\helpers\Html;
$this->title = 'Posts';
?>
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Create Post', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $post): ?>
<tr>
<td><?= Html::encode($post->title) ?></td>
<td>
<?= Html::a('Update', ['update', 'id' => $post->id]) ?>
<?= Html::a('Delete', ['delete', 'id' => $post->id], [
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
create.php
Buat file views/post/create.php
.
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$this->title = 'Create Post';
?>
<h1><?= Html::encode($this->title) ?></h1>
<div class="post-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'content')->textarea(['rows' => 6]) ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
update.php
Buat file views/post/update.php
.
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$this->title = 'Update Post: ' . $model->title;
?>
<h1><?= Html::encode($this->title) ?></h1>
<div class="post-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'content')->textarea(['rows' => 6]) ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
4. Database Migration
Pastikan Anda memiliki tabel post
di database Anda. Berikut adalah contoh migrasi untuk membuat tabel tersebut.
use yii\db\Migration;
class m210101_000001_create_post_table extends Migration
{
public function up()
{
$this->createTable('post', [
'id' => $this->primaryKey(),
'title' => $this->string()->notNull(),
'content' => $this->text()->notNull(),
]);
}
public function down()
{
$this->dropTable('post');
}
}
0 Komentar