You are on page 1of 4

Yii

1. Menampilan pesan

Buat view
<?php
use yii\helpers\Html;
?>
<?= Html::encode($message) ?>

Buat controller
class SiteController extends Controller
{
public function actionSay($message = 'Hello')
{
return $this->render('say', ['message' => $message]);
}
}
Controller diata akan menaktifkan views saya dengan parameter message=$message, dimana
$message diisi parameter get dari pengguna, atau bila kosong akan diisi ‘Hello’
Untuk mengaktifkan controller ini pada URL ditulis localhost/yii/web/index.php?r=site/say
2. Pemrosesan Form
Buat model EntryForm.php
<?php

namespace app\models;
use yii\base\Model;
class EntryForm extends Model
{
public $name;
public $email;
public function rules()
{
return [
[['name', 'email'], 'required'],
['email', 'email'],
];
}
}
Rules() digunakan untuk validasi
Buat controller actionEntry
public function actionEntry()
{
$model = new EntryForm();
if ($model->load(Yii::$app->request->post()) && $model-
>validate())
{
// valid data received in $model
// do something meaningful here about $model ...
return $this->render('entry-confirm', ['model' => $model]);
} else {

return $this->render('entry', ['model' => $model]);


}
}

Buat objek EntryForm


Mapping objek EntryForm dengan data dari parameter post dan validasi isi objek EntryForm
Jika gagal, tampilkan view entry-confirm dan bila berhasil tampilkan view entry

Buat view
Entry-confirm
<?php
use yii\helpers\Html;
?>
<p>You have entered the following information:</p>
<ul>
<li><label>Name</label>: <?= Html::encode($model->name) ?></li>
<li><label>Email</label>: <?= Html::encode($model->email) ?></li>
</ul>

Entry
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary'])
?>
</div>
<?php ActiveForm::end(); ?>
Bekerja dengan database
1. Setting db pada config/db.php
<?php

return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
];
2. Buat model active record(Nama kelas sama dengan nama table)
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Country extends ActiveRecord
{
}
3. Membuat controller
<?php
namespace app\controllers;
use yii\web\Controller;
use yii\data\Pagination;
use app\models\Orang;
class OrangController extends Controller
{
public function actionIndex()
{
$query = Orang::find();
$pagination = new Pagination([
'defaultPageSize' => 5,
'totalCount' => $query->count(),
]);
$orang = $query->orderBy('nama')->offset($pagination->offset)
->limit($pagination->limit)->all();
return $this->render('index', [
'orang' => $orang,
'pagination' => $pagination,
]);
}
}

4. Membuat view
Dalam view buat folder sesuai “r” nya, beri nama index.php

<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Countries</h1>
<ul>
<?php foreach ($orang as $country): ?>
<li>
<?= Html::encode("{$country->Nama} ({$country->Alamat})") ?></li>

<?php endforeach; ?>


</ul>
<?= LinkPager::widget(['pagination' => $pagination]) ?>

5.

You might also like