一、目录结构:
模型类:models/
视图文件:views/
二、MVC示例:
在controllers下的SiteController.php里新增方法:
//action后面的操作映射为say-hello
public function actionSayHello($message='World'){
return $this->render('say',['message'=>$message]);
}
在views/site下新建视图say.php,内容:
use yii\helpers\Html;
=Html::encode($message)
输入网址:http://test.com/index.php?r=site/say-hello&message=OK
查看输出结果
三、Form示例
修改SiteControlloer.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:
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:
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form=ActiveForm::begin();
= $form->field($model,'name')
= $form->field($model,'email')
<div class="form-group">
=Html::submitButton('Submit',['class'=>'btn btn-primary'])
</div>
ActiveForm::end();
新增视图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)
</ul>
现在目录结构是这样的:
测试:http://test.com/index.php?r=site/entry