Showing posts with label yii2. Show all posts
Showing posts with label yii2. Show all posts

Thursday, July 21, 2016

003 Create form input

Goal : form input  - menerima data dari user - tampilkan lagi utk konfirmasi

1.buat model, dg variable public name dan email
ada rule name, email yg required dan email yg benar


namespace frontend\models;

use Yii;
use yii\base\Model;

class EntryForm extends Model
{
    public $name;
    public $email;

    public function rules()
    {
        return [
            [['name', 'email'], 'required'],
            ['email', 'email'],
        ];
    }
}

2. buat action di siteController

    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 {
            // either the page is initially displayed or there is some validation error
            return $this->render('entry', ['model' => $model]);
        }
    }

tampak di sana ada 2 view = entry dan entry-confirm

use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>


field($model, 'name')->label('Your Name') ?>
field($model, 'email')->label('Your Email') ?>

   

        'btn btn-primary']) ?>
   


ActiveForm menghandle dengan baik, perhatikan begin() dan end()

utk entry-confirm:

use yii\helpers\Html;
?>
You have entered the following information:



       
  • : name) ?>

  •    
  • : email) ?>






Wednesday, July 20, 2016

002 Create Action

Action = object yg bisa di eksekusi oleh end-user
- di group/masuk dlm controller (misal site controller)
cara nulis, contoh : action[nama action nya dimulai huruf besar]
  actionSay
  actionCreateComment


namespace app\controllers;

use yii\web\Controller;

class SiteController extends Controller
{
    // ...existing code...

    public function actionSay($message = 'Hello')
    {
        return $this->render('say', ['message' => $message]);
    }
}

 fungsi render = memanggil view di parameter ke 1, berarti file 'say'
file view disimpan di views/site/say.php

maka header dan footer akan tetap sama, isinya yg berubah
tampilan diambil dari : views/layouts/main.php

kalau Controller cara nulisnya : namaController, contoh : siteController.php


001 Install Yii versi 2

composer = management tool utk dependency, jadi bisa install dependency otomatis bila tidak ada.

install dari yii-advanced-app-2.0.9.tgz

extract

copy semua ke folder www

buka cmd, jalankan = php         init
contoh:
/xampp/php/php.exe        /Users/user/Documents/www/yii2/yii-advanced-app-2.0.9/advanced/init

pilih dev

create database, konfigure dbname dan password di common/config/main-local.php

isi di \xampp\apache\conf\extra\httpd-vhosts.conf


  
       ServerName frontend.dev
       DocumentRoot "/path/to/yii-application/frontend/web/"

      
           # use mod_rewrite for pretty URL support
           RewriteEngine on
           # If a directory or a file exists, use the request directly
           RewriteCond %{REQUEST_FILENAME} !-f
           RewriteCond %{REQUEST_FILENAME} !-d
           # Otherwise forward the request to index.php
           RewriteRule . index.php

           # use index.php as index file
           DirectoryIndex index.php

           # ...other settings...
      

  


  
       ServerName backend.dev
       DocumentRoot "/path/to/yii-application/backend/web/"

      
           # use mod_rewrite for pretty URL support
           RewriteEngine on
           # If a directory or a file exists, use the request directly
           RewriteCond %{REQUEST_FILENAME} !-f
           RewriteCond %{REQUEST_FILENAME} !-d
           # Otherwise forward the request to index.php
           RewriteRule . index.php

           # use index.php as index file
           DirectoryIndex index.php

           # ...other settings...
      

  


restart apache, run : http://frontend.dev
ada 2 webroot: http://frontend.dev dan http://backend.dev