摘要:
Yii2-application用法 (Yii::$app用法) 配置文件 config\web.php return [ 'id' => 'app-frontend', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'control 阅读全文
posted @ 2024-03-30 14:56
胡勇健
阅读(281)
评论(0)
推荐(0)
摘要:
Yii2 Action用法 actions方法 public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ 'class' => 'yii\captcha\ 阅读全文
posted @ 2024-03-30 14:55
胡勇健
阅读(56)
评论(0)
推荐(0)
摘要:
yii2 Cookie 读取 Cookies $cookies = Yii::$app->request->cookies; // 获取名为 "language" cookie 的值,如果不存在,返回默认值"en" $language = $cookies->getValue('language', 阅读全文
posted @ 2024-03-30 14:55
胡勇健
阅读(26)
评论(0)
推荐(0)
摘要:
yii2 session 开启和关闭Sessions $session = Yii::$app->session; // 检查session是否开启 if ($session->isActive) ... // 开启session $session->open(); // 关闭session $se 阅读全文
posted @ 2024-03-30 14:54
胡勇健
阅读(77)
评论(0)
推荐(0)
摘要:
Yii2-助手类(formatter) 示例 echo \Yii::$app->formatter->asDatetime(time(),'Y-M-d H:i:s'); //2023-05-12 12:32:32 echo \Yii::$app->formatter->asRelativeTime( 阅读全文
posted @ 2024-03-30 14:53
胡勇健
阅读(30)
评论(0)
推荐(0)
摘要:
Yii2 ArrayHelper助手 数组助手类 ArrayHelper Yii 数组助手类提供了额外的静态方法,让你更高效的处理数组。 模型转数组 $model = Country::findOne(['code'=>'BR']); ArrayHelper::toArray($model); // 阅读全文
posted @ 2024-03-30 14:52
胡勇健
阅读(197)
评论(0)
推荐(0)
摘要:
Yii2-助手类(Html) 引入命名空间 use yii\helpers\Html; 使用 <?php //【一】表单:Html::beginForm(提交地址,提交方法,属性数组);?> <?=Html::beginForm('','post',['id'=>'form','class'=>'f 阅读全文
posted @ 2024-03-30 14:50
胡勇健
阅读(52)
评论(0)
推荐(0)
摘要:
Yii2-助手(StringHelper) 截断字符串 echo StringHelper::truncate('中文截断字符',4); //'中文截断...' 字符串转数组 StringHelper::explode('this is string',' '); //[ 0 => 'this' 1 阅读全文
posted @ 2024-03-30 14:49
胡勇健
阅读(30)
评论(0)
推荐(0)
摘要:
yii2-分页 使用ActiveDataProvider 和 GridView controller public function actionIndex(){ $dataProvider = new ActiveDataProvider([ 'query' => ArticleClass::fi 阅读全文
posted @ 2024-03-30 14:46
胡勇健
阅读(64)
评论(0)
推荐(0)
摘要:
分页 使用ActiveDataProvider 和 GridView controller public function actionIndex(){ $dataProvider = new ActiveDataProvider([ 'query' => ArticleClass::find(), 阅读全文
posted @ 2024-03-30 14:44
胡勇健
阅读(23)
评论(0)
推荐(0)
摘要:
yii2 密码和加解密 生成hash $hash = Yii::$app->getSecurity()->generatePasswordHash($password); 验证hash if (Yii::$app->getSecurity()->validatePassword($password, 阅读全文
posted @ 2024-03-30 14:43
胡勇健
阅读(135)
评论(0)
推荐(0)
摘要:
yii2-用户认证 配置 yii\web\User return [ 'components' => [ 'user' => [ 'identityClass' => 'app\models\User', 'enableAutoLogin' => true, 'loginUrl' => ['user 阅读全文
posted @ 2024-03-30 14:42
胡勇健
阅读(56)
评论(0)
推荐(0)
摘要:
Yii2 过滤器 过滤器AccessController AccessControl 基于一组规则提供简单的访问控制 /** * {@inheritdoc} */ public function behaviors() { return [ 'access' => [ 'class' => Acce 阅读全文
posted @ 2024-03-30 14:41
胡勇健
阅读(67)
评论(0)
推荐(0)
摘要:
yii2-队列使用 安装yii2-queue composer require yiisoft/yii2-queue 安装yii2-redis composer require yiisoft/yii2-redis 控制台配置文件 console.php 和 web.php 'redis' => [ 阅读全文
posted @ 2024-03-30 14:40
胡勇健
阅读(358)
评论(0)
推荐(0)
摘要:
Yii2验证器Validator 控制器 public function actionIndex() { $data = ['LoginForm'=>['username'=>'huyongjian','password'=>'1234']]; $model = new LoginForm(); $ 阅读全文
posted @ 2024-03-30 14:38
胡勇健
阅读(113)
评论(0)
推荐(0)
摘要:
yii2事件 事件可以将自定义代码“注入”到现有代码中的特定执行点。 1 yii\base\Event 封装了与时间相关的有关数据并提供了一些功能函数作为辅助 class Event extends Object { public $name; // 事件名 public $sender; // 事 阅读全文
posted @ 2024-03-30 14:37
胡勇健
阅读(54)
评论(0)
推荐(0)
摘要:
yii2依赖注入 所谓依赖注入,实质上就是当某个类对象需要使用另一个类实例的时候,不在类内部实例化另一个类,而将实例化的过程放在类外面实现,实例化完成后再赋值给类对象的某个属性。 这样的话该类不需要知道赋值给它的属性的对象具体属于哪个类的,当需要改变这个属性的类型的时候,无需对这个类的代码进行任何改 阅读全文
posted @ 2024-03-30 14:35
胡勇健
阅读(72)
评论(0)
推荐(0)
摘要:
yii2 小部件 小部件基本上在views中使用,在视图中可调用 yii\base\Widget::widget() 方法使用小部件。 继承 yii\base\Widget 类并覆盖 yii\base\Widget::init() 和/或 yii\base\Widget::run() 方法可创建小部 阅读全文
posted @ 2024-03-30 14:34
胡勇健
阅读(20)
评论(0)
推荐(0)
摘要:
yii2 扩展 示例 安装扩展 composer require --prefer-dist yiisoft/yii2-imagine 使用扩展 use yii\imagine\Image; public function actionIndex() { Image::thumbnail('@web 阅读全文
posted @ 2024-03-30 14:31
胡勇健
阅读(18)
评论(0)
推荐(0)
摘要:
yii2数据库访问对象 配置数据库链接 $db = new yii\db\Connection([ 'dsn' => 'mysql:host=localhost;dbname=xhj', 'username' => 'root', 'password' => '123456', 'charset' 阅读全文
posted @ 2024-03-30 14:29
胡勇健
阅读(23)
评论(0)
推荐(0)
摘要:
yii2响应(Responses) 状态码 Yii::$app->response->statusCode = 200; 异常 yii\web\BadRequestHttpException: status code 400. yii\web\ConflictHttpException: statu 阅读全文
posted @ 2024-03-30 14:20
胡勇健
阅读(62)
评论(0)
推荐(0)
摘要:
yii2服务定位器 服务定位器是注册和访问组件的对象 注册组件 use yii\di\ServiceLocator; use yii\caching\FileCache; $locator = new ServiceLocator; // 通过一个可用于创建该组件的类名,注册 "cache" (缓存 阅读全文
posted @ 2024-03-30 14:17
胡勇健
阅读(15)
评论(0)
推荐(0)
摘要:
yii2模块 模块是独立的软件单元,由模型, 视图, 控制器和其他支持组件组成, 终端用户可以访问在应用主体中已安装的模块的控制器, 模块被当成小应用主体来看待,和应用主体不同的是, 模块不能单独部署,必须属于某个应用主体。 模块文件结构 modules admin controllers Admi 阅读全文
posted @ 2024-03-30 14:13
胡勇健
阅读(24)
评论(0)
推荐(0)
摘要:
Yii2行为用法 使用行为(behavior)可以在不修改现有类的情况下,对类的功能进行扩充 行为类 (app\common\behaviors\MyBehavior) <?php namespace app\common\behaviors; use yii\base\Behavior; clas 阅读全文
posted @ 2024-03-30 14:11
胡勇健
阅读(25)
评论(0)
推荐(0)
摘要:
yii2 rules验证规则大全 required : 必须值验证属性 [['name','email'],'required'] [['name'],'required','message'=>'提示信息'] email : 邮箱验证 [['email'], 'email','message'=> 阅读全文
posted @ 2024-03-30 14:08
胡勇健
阅读(161)
评论(0)
推荐(0)
摘要:
yii2表单使用 模型 models/form/EntryForm.php <?php namespace app\models\form; use yii\base\Model; class EntryForm extends Model { public $name; public $email 阅读全文
posted @ 2024-03-30 14:06
胡勇健
阅读(23)
评论(0)
推荐(0)
摘要:
yii2请求组件 应用的请求是用 yii\web\Request 对象来表示的 请求参数 $request = Yii::$app->request; $get = $request->get(); // 等价于: $get = $_GET; $id = $request->get('id'); / 阅读全文
posted @ 2024-03-30 14:03
胡勇健
阅读(35)
评论(0)
推荐(0)
摘要:
yii2 资源 AppAsset.php <?php namespace app\assets; use yii\web\AssetBundle; class AppAsset extends AssetBundle { public $basePath = '@webroot'; public $ 阅读全文
posted @ 2024-03-30 14:00
胡勇健
阅读(13)
评论(0)
推荐(0)
摘要:
yii2过滤器 过滤器是 控制器 动作 执行之前或之后执行的对象。 示例 过滤器 common\components\ActionTimeFilter <?php namespace app\common\components; use Yii; use yii\base\ActionFilter; 阅读全文
posted @ 2024-03-30 13:57
胡勇健
阅读(215)
评论(0)
推荐(0)
摘要:
yii2视图 示例代码 <?php use yii\helpers\Html; use yii\widgets\ActiveForm; /* @var $this yii\web\View */ /* @var $form yii\widgets\ActiveForm */ /* @var $mod 阅读全文
posted @ 2024-03-30 13:53
胡勇健
阅读(26)
评论(0)
推荐(0)
摘要:
yii2 模型 Yii2的模型(Model)是MVC(Model-View-Controller)设计模式中的一部分,它代表业务数据、规则和逻辑的对象。模型通常用于处理与数据相关的业务逻辑,如数据的验证、访问和修改等。 模型示例代码 <?php namespace app\models; use Y 阅读全文
posted @ 2024-03-30 11:43
胡勇健
阅读(72)
评论(0)
推荐(0)
摘要:
yii2控制器 Yii2 的控制器(Controller)是 MVC(Model-View-Controller)设计模式中的核心组件之一,负责处理用户请求并生成相应的响应。 控制器包含了处理请求所需的方法(通常称为动作方法或动作),并可以调用模型和视图来执行相应的业务逻辑和展示内容。 Yii2 控 阅读全文
posted @ 2024-03-30 11:09
胡勇健
阅读(63)
评论(0)
推荐(0)
摘要:
yii2 Gii使用和自定义模板 配置开启gii config/web.php 添加代码 if (YII_ENV_DEV) { $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = [ 'class' => 'yii\gii\Modu 阅读全文
posted @ 2024-03-30 11:01
胡勇健
阅读(126)
评论(0)
推荐(0)
摘要:
yii2 配置bootstrap使用 配置 config/web.php 添加 people <?php ... $config = [ ... 'bootstrap' => ['log','people'], ... 'components' => [ 'people'=> [ 'class'=> 阅读全文
posted @ 2024-03-30 10:57
胡勇健
阅读(68)
评论(0)
推荐(0)
摘要:
yii2 配置文件详情 简化的 web.php 配置文件 <?php return [ 'id' => 'app-id', // 应用程序的唯一标识符 'basePath' => dirname(__DIR__), // 应用程序的基础路径 'bootstrap' => ['log'], // 在应 阅读全文
posted @ 2024-03-30 10:39
胡勇健
阅读(113)
评论(0)
推荐(0)
摘要:
yii2默认语言 全局配置 config/web.php 添加language $config = [ 'id' => 'basic', 'basePath' => dirname(__DIR__), 'language' => 'zh-CN', //全局设置为中文--推荐 ] 局部配置 Yii:: 阅读全文
posted @ 2024-03-30 10:24
胡勇健
阅读(29)
评论(0)
推荐(0)
摘要:
yii2安装 安装composer curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer 安装yii2-basic composer create-project --pre 阅读全文
posted @ 2024-03-30 10:10
胡勇健
阅读(27)
评论(0)
推荐(0)

浙公网安备 33010602011771号