学习php的yii框架_对yii的理解

yii版本:yii-basic-app-2.0.12


入口文件(inde.php)


index.php 中的require(__DIR__ . '/../vendor/autoload.php');的理解:__DIR__魔术常量代表当前运行文件所在的目录F:\phpStudy\WWW\yii2\web\
new yii\web\Application($config))->run();//启动WebApplication
$config = require(__DIR__ . '/../config/web.php');//加载web配置信息

调用过程细节。

<?php//控制器(controllers)中的/controller/BaseController.php
namespace app\controllers;
use app\models\system\AdminLog;
//使用了模型(models)中的/models/system/AdminLog.php的AdminLog类

class BaseController extends Controller{}
<?php//模型(models)中的/models/system/AdminLog.php
namespace app\models\system;
use Yii;
class AdminLog extends \yii\db\ActiveRecord{}

 

模型(models)

public function rules()//models中的rules部分是一些表单的验证规则
{
    return [
            [['name', 'email'], 'required'],
            ['email', 'email'],
        ];
}


返回的是数组,required表示为字段必填。每个[]的最后一个值为参数,['email', 'email']为email类型。
系统默认的验证规则:
字符串长度[[ 'url', 'user_agent', 'admin_email', 'ip'], 'string', 'max' => 255]
去除首尾空白字符['email', 'trim']或['email', 'filter', 'filter' => 'trim']
赋予默认值['age', 'default', 'value' => 18]
整数格式['age', 'integer']
验证码['verificationCode', 'captcha']
文件上传['primaryImage', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024*1024*1024]
image :是否为有效的图片文件['primaryImage','image', 'extensions' => 'png, jpg,jpeg','minWidth' => 100,'maxWidth' => 1000,'minHeight' =>100,'maxHeight' => 1000,]

<?php
class AdminLog extends \yii\db\ActiveRecord{
    public static function tableName(){
        return 'admin_log';//类名和数据表名不能直接对应,覆写tableName()方法去显式指定相关表名。
    }
    public function primaryKey(){
        return 'user_id';//自定义主键为user_id
    }
    public function attributeLabels()//可以不用设置
    {//模型中attributeLabels()函数作用:hint作用,即表单中用户插入数据时,提示用户该字段该填什么内容
        return [
            'id' => 'ID',//id为数据表中的字段名,ID为表单显示的描述
            'name' => '用户名',
        ];
    }
}

 

模型中extends \yii\db\ActiveRecord{}里的成员方法参考:http://www.yiichina.com/doc/api/2.0/yii-db-activerecord
比如:primaryKey()返回此AR类的主键名称、attributes()返回的列表的所有属性名称的模型。


提交表单:

<?= $form->field($verify,'verifyCode') //field()方法生成输入框的文字标签,verifyCode(翻译为标签)

$verify 模型在siteController中$verify = new Captcha();

siteController中return $this->render('login',['verify' => $verify]);//符合什么条件渲染模型$verify到site/login中。

控制器(controllers)

控制器中$this->redirect('/');会跳转到站点根,localhost,会跳转到http://localhost/
Url::toRoute('/site/index', true);

<?php
namespace app\controllers;
class BaseController extends Controller{
    public function beforeAction($action)
    {//加载在控制的action前的操作。(做过滤验证之类)
    }
}

控制器中,选择布局的方式,在控制器成员方法中:$this->layout = "main"; //设置使用的布局文件
加载的布局文件在/views/layouts/main.php

路由(route)


路由使用如下格式:ControllerID/ActionID
如果属于模块下的控制器,使用如下格式:ModuleID/ControllerID/ActionID;
post-comment 对应 app\controllers\PostCommentController;

布局(layout)


return $this->render('index');view/index.php但是没有<html><head><title>等标签,因为应用了布局
可以使用$this->layout = "lte_main_login";即调用view/layout/lte_main_login.php文件
不设定layout的值,默认使用main.php布局文件
布局文件是用来解决相同部分的问题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值