Yii2 详解MVC(模型-视图-控制器)

jerry Yii2 2015年11月15日 收藏

一、目录结构:

模型类:models/

视图文件:views/

二、MVC示例:

在controllers下的SiteController.php里新增方法:

    //action后面的操作映射为say-hello  
    public function actionSayHello($message='World'){  
        return $this->render('say',['message'=>$message]);  
    }

在views/site下新建视图say.php,内容:

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

输入网址:http://test.com/index.php?r=site/say-hello&message=OK 

查看输出结果

三、Form示例

修改SiteControlloer.php:

<?php  
      
    namespace app\controllers;  
      
    use Yii;  
    use yii\filters\AccessControl;  
    use yii\web\Controller;  
    use yii\filters\VerbFilter;  
    use app\models\LoginForm;  
    use app\models\ContactForm;  
    use app\models\EntryForm;  
      
    class SiteController extends Controller  
    {  
        public function behaviors()  
        {  
            return [  
                'access' => [  
                    'class' => AccessControl::className(),  
                    'only' => ['logout'],  
                    'rules' => [  
                        [  
                            'actions' => ['logout'],  
                            'allow' => true,  
                            'roles' => ['@'],  
                        ],  
                    ],  
                ],  
                'verbs' => [  
                    'class' => VerbFilter::className(),  
                    'actions' => [  
                        'logout' => ['post'],  
                    ],  
                ],  
            ];  
        }  
      
        public function actions()  
        {  
            return [  
                'error' => [  
                    'class' => 'yii\web\ErrorAction',  
                ],  
                'captcha' => [  
                    'class' => 'yii\captcha\CaptchaAction',  
                    'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,  
                ],  
            ];  
        }  
      
        public function actionIndex()  
        {  
            return $this->render('index');  
        }  
      
        public function actionLogin()  
        {  
            if (!\Yii::$app->user->isGuest) {  
                return $this->goHome();  
            }  
      
            $model = new LoginForm();  
            if ($model->load(Yii::$app->request->post()) && $model->login()) {  
                return $this->goBack();  
            } else {  
                return $this->render('login', [  
                    'model' => $model,  
                ]);  
            }  
        }  
      
        public function actionLogout()  
        {  
            Yii::$app->user->logout();  
      
            return $this->goHome();  
        }  
      
        public function actionContact()  
        {  
            $model = new ContactForm();  
            if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {  
                Yii::$app->session->setFlash('contactFormSubmitted');  
      
                return $this->refresh();  
            } else {  
                return $this->render('contact', [  
                    'model' => $model,  
                ]);  
            }  
        }  
      
        public function actionAbout()  
        {  
            return $this->render('about');  
        }  
      
        //action后面的操作映射为say-hello  
        public function actionSayHello($message='World'){  
            return $this->render('say',['message'=>$message]);  
        }  
        public function actionEntry(){  
            $model=new EntryForm;  
            if($model->load(Yii::$app->request->post()) && $model->validate()){  
                return $this->render('entry-confirm',['model'=>$model]);  
            }else  
            {  
                return $this->render('entry',['model'=>$model]);  
            }  
        }  
    }

新增ModelEntryForm.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']  
                      
            ];  
        }  
    }

新增视图entry.php:

<?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();?>

新增视图entry-confirm.php:

<?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)?>  
    </ul>

现在目录结构是这样的:

20150121232326939.png

测试:http://test.com/index.php?r=site/entry

20150121232332500.png