Setelah mengulas mengenai menu About Yii, berikut kita akan membahas menu Contact yang ada di dalam aplikasi dasar Yii.

Pertama-tama kita melihat link untuk menu Contact yaitu http://yii/coba/index.php?r=site/contact

  1. Di sini kita melihat bahwa controllernya adalah SiteController dan actionnya adalah actionContact
    Pada file ./public_html/coba/protected/controllers/SiteController.php ada coding seperti berikut:
    //start———————————————————————————————–
        public function actionContact()
        {
            $model=new ContactForm;
            if(isset($_POST[‘ContactForm’]))
            {
                $model->attributes=$_POST[‘ContactForm’];
                if($model->validate())
                {
                    $headers=”From: {$model->email}\r\nReply-To: {$model->email}”;
                    mail(Yii::app()->params[‘adminEmail’],$model->subject,$model->body,$headers);
                    Yii::app()->user->setFlash(‘contact’,’Thank you for contacting us. We will respond to you as soon as possible.’);
                    $this->refresh();
                }
            }
            $this->render(‘contact’,array(‘model’=>$model));
        }
    //end———————————————————————————————–
  2. Di situ ada object dari kelas ContactForm. Kelas ini ada di ./public_html/coba/protected/models/ContactForm.php yang berisi:
    //start———————————————————————————————–
    /**
     * ContactForm class.
     * ContactForm is the data structure for keeping
     * contact form data. It is used by the ‘contact’ action of ‘SiteController’.
     */
    class ContactForm extends CFormModel
    {
        public $name;
        public $email;
        public $subject;
        public $body;
        public $verifyCode;

        /**
         * Declares the validation rules.
         */
        public function rules()
        {
            return array(
                // name, email, subject and body are required
                array(‘name, email, subject, body’, ‘required’),
                // email has to be a valid email address
                array(’email’, ’email’),
                // verifyCode needs to be entered correctly
                array(‘verifyCode’, ‘captcha’, ‘allowEmpty’=>!CCaptcha::checkRequirements()),
            );
        }

        /**
         * Declares customized attribute labels.
         * If not declared here, an attribute would have a label that is
         * the same as its name with the first letter in upper case.
         */
        public function attributeLabels()
        {
            return array(
                ‘verifyCode’=>’Verification Code’,
            );
        }
    }
    //end———————————————————————————————–

  3. Pada point 1 perhatikan coding :
            $this->render(‘contact’,array(‘model’=>$model));

    Dari sini tampilan diarahkan ke ./public_html/coba/protected/views/site/contact.php yang isinya:
    //start———————————————————————————————–
    <?php
    $this->pageTitle=Yii::app()->name . ‘ – Contact Us’;
    $this->breadcrumbs=array(
        ‘Contact’,
    );
    ?>

    <h1>Contact Us</h1>

    <?php if(Yii::app()->user->hasFlash(‘contact’)): ?>

    <div class=”flash-success”>
        <?php echo Yii::app()->user->getFlash(‘contact’); ?>
    </div>

    <?php else: ?>

    <p>
    If you have business inquiries or other questions, please fill out the following form to contact us. Thank you.
    </p>

    <div class=”form”>

    <?php $form=$this->beginWidget(‘CActiveForm’); ?>

        <p class=”note”>Fields with <span class=”required”>*</span> are required.</p>

        <?php echo $form->errorSummary($model); ?>

        <div class=”row”>
            <?php echo $form->labelEx($model,’name’); ?>
            <?php echo $form->textField($model,’name’); ?>
        </div>

        <div class=”row”>
            <?php echo $form->labelEx($model,’email’); ?>
            <?php echo $form->textField($model,’email’); ?>
        </div>

        <div class=”row”>
            <?php echo $form->labelEx($model,’subject’); ?>
            <?php echo $form->textField($model,’subject’,array(‘size’=>60,’maxlength’=>128)); ?>
        </div>

        <div class=”row”>
            <?php echo $form->labelEx($model,’body’); ?>
            <?php echo $form->textArea($model,’body’,array(‘rows’=>6, ‘cols’=>50)); ?>
        </div>

        <?php if(CCaptcha::checkRequirements()): ?>
        <div class=”row”>
            <?php echo $form->labelEx($model,’verifyCode’); ?>
            <div>
            <?php $this->widget(‘CCaptcha’); ?>
            <?php echo $form->textField($model,’verifyCode’); ?>
            </div>
            <div class=”hint”>Please enter the letters as they are shown in the image above.
            <br/>Letters are not case-sensitive.</div>
        </div>
        <?php endif; ?>

        <div class=”row buttons”>
            <?php echo CHtml::submitButton(‘Submit’); ?>
        </div>

    <?php $this->endWidget(); ?>

    </div><!– form –>

    <?php endif; ?>
    //end———————————————————————————————–

  4. Tampilan akan muncul sebagai berikut:

Tutorial berikutnya adalah analisa menu Login Yii.

Kunjungi www.proweb.co.id untuk menambah wawasan anda.

Analisa menu contact Yii