Yii2 使用二 了解MVC
来源:互联网 发布:cdn域名真实ip 编辑:程序博客网 时间:2024/05/17 08:32
一、目录结构:
模型类:models/
视图文件:views/
二、MVC示例:
在controllers下的SiteController.php里新增方法:
//action后面的操作映射为say-hello public function actionSayHello($message='World'){ return $this->render('say',['message'=>$message]); }
在views/site下新建视图say.php,内容:
<?phpuse yii\helpers\Html;?><?=Html::encode($message)?>
输入网址:http://test.com/index.php?r=site/say-hello&message=OK
查看输出结果
三、Form示例
修改SiteControlloer.php:
<?phpnamespace 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:
<?phpnamespace 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:
<?phpuse 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>现在目录结构是这样的:
测试:http://test.com/index.php?r=site/entry
0 1
- Yii2 使用二 了解MVC
- YII2下使用百度编辑器扩展yii2-ueditor(二)
- 深入了解yii2框架
- yii2 如何使用数据库(二) 之使用ActiveRecord
- 【MVC】了解
- yii2使用
- Yii2 yii2-imagine的使用
- Yii2的MVC新特性
- yii2.0 分页(二)
- yii2基础(二)
- yii2.0 分页(二)
- YII2 GirdView详解(二)
- LoaderManager使用详解(二)---了解LoaderManager
- LoaderManager使用详解(二)---了解LoaderManager
- LoaderManager使用详解(二)---了解LoaderManager
- LoaderManager使用详解(二)---了解LoaderManager
- LoaderManager使用详解(二)---了解LoaderManager
- LoaderManager使用详解(二)---了解LoaderManager
- 程序员如何提升自己的代码质量?
- 4W家庭理财 V2.2
- 【java基础】常用API和集合框架以及基本类型包装类
- 推荐Maven Repository网站
- mysql general_log评测
- Yii2 使用二 了解MVC
- 4W家庭理财 V2.3
- Objective-C中的Block
- Callable与Future
- 泛型中<? extends T>和<? super T> 差别
- 为什么一个程序终止会导致程序输出的最后几行消失
- redis删除单个key和多个key
- CSS 基础知识(二)
- hibernate事物隔离机制