🗣 Wd提问: yii2
🤖 Ai回答: Yii2 是一个高性能的 PHP 框架,用于快速开发 Web 应用程序。以下是 Yii2 的一些核心概念和使用示例:
1、安装 Yii2
使用 Composer 安装基础应用模板
bash
composer create-project prefer-dist yiisoft/yii2-app-basic basic
安装高级应用模板
bash
composer create-project prefer-dist yiisoft/yii2-app-advanced advanced
2、目录结构
basic/
├── assets/ 资源文件
├── commands/ 控制台命令
├── config/ 配置文件
├── controllers/ 控制器
├── models/ 模型
├── runtime/ 运行时文件
├── tests/ 测试文件
├── vendor/ Composer 依赖
├── views/ 视图文件
├── web/ Web 入口目录
└── yii 控制台入口脚本
3、基本组件
控制器 (Controller)
php
<?php
namespace app\controllers;
use yii\web\Controller;
use yii\web\Response;
class SiteController extends Controller
{
public function actionIndex()
{
return $this-render(&039;index&039;);
}
public function actionHello($name = &039;World&039;)
{
return Hello, 、$name;
}
}
模型 (Model)
php
<?php
namespace app\models;
use yii\base\Model;
class ContactForm extends Model
{
public $name;
public $email;
public $subject;
public $body;
public function rules()
{
return [
[[&039;name&039;, &039;email&039;, &039;subject&039;, &039;body&039;], &039;required&039;],
[&039;email&039;, &039;email&039;],
];
}
public function attributeLabels()
{
return [
&039;name&039; = &039;Your Name&039;,
&039;email&039; = &039;Email Address&039;,
&039;subject&039; = &039;Subject&039;,
&039;body&039; = &039;Message&039;,
];
}
}
视图 (View)
php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$this-title = &039;Contact&039;;
?
<h1<?= Html::encode($this-title) ?</h1
<?php $form = ActiveForm::begin(); ?
<?= $form-field($model, &039;name&039;) ?
<?= $form-field($model, &039;email&039;) ?
<?= $form-field($model, &039;subject&039;) ?
<?= $form-field($model, &039;body&039;)-textarea([&039;rows&039; = 6]) ?
<div class=form-group
<?= Html::submitButton(&039;Submit&039;, [&039;class&039; = &039;btn btn-primary&039;]) ?
</div
<?php ActiveForm::end(); ?
4、数据库操作 (ActiveRecord)
定义模型
php
<?php
namespace app\models;
use yii\db\ActiveRecord;
class User extends ActiveRecord
{
public static function tableName()
{
return &039;user&039;;
}
public function rules()
{
return [
[[&039;username&039;, &039;email&039;], &039;required&039;],
[&039;email&039;, &039;email&039;],
[&039;username&039;, &039;unique&039;],
];
}
}
查询数据
php
// 查找单个记录
$user = User::findOne(1);
$user = User::find()-where([&039;username&039; = &039;john&039;])-one();
// 查找多个记录
$users = User::find()-all();
$users = User::find()-where([&039;status&039; = 1])-all();
// 分页查询
$users = User::find()-limit(10)-offset(20)-all();
// 计数
$count = User::find()-count();
保存数据
php
$user = new User();
$user-username = &039;john&039;;
$user-email = &039;john@65.hk&039;;
$user-save();
// 更新
$user = User::findOne(1);
$user-email = &039;newemail@65.hk&039;;
$user-save();
// 删除
$user = User::findOne(1);
$user-delete();
5、路由和 URL
URL 管理
php
// 生成 URL
echo Url::to([&039;site/index&039;]); // /index.php?r=site/index
echo Url::to([&039;post/view&039;, &039;id&039; = 100]); // /index.php?r=post/view&id=100
// 在视图中使用
echo Html::a(&039;Link Text&039;, [&039;site/about&039;]);
URL 规则配置 (config/web.php)
php
&039;urlManager&039; = [
&039;enablePrettyUrl&039; = true,
&039;showScriptName&039; = false,
&039;rules&039; = [
&039;post/<id:\d+&039; = &039;post/view&039;,
&039;posts&039; = &039;post/index&039;,
],
],
6、表单验证和处理
php
public function actionCreate()
{
$model = new Post();
if ($model-load(Yii::$app-request-post()) && $model-save()) {
Yii::$app-session-setFlash(&039;success&039;, &039;Post created successfully!&039;);
return $this-redirect([&039;view&039;, &039;id&039; = $model-id]);
}
return $this-render(&039;create&039;, [
&039;model&039; = $model,
]);
}
7、组件和小部件
使用 GridView
php
use yii\grid\GridView;
echo GridView::widget([
&039;dataProvider&039; = $dataProvider,
&039;columns&039; = [
&039;id&039;,
&039;name&039;,
&039;email&039;,
[
&039;class&039; = &039;yii\grid\ActionColumn&039;,
&039;template&039; = &039;{view} {update} {delete}&039;,
],
],
]);
使用 ActiveForm
php
use yii\widgets\ActiveForm;
$form = ActiveForm::begin([
&039;id&039; = &039;login-form&039;,
&039;options&039; = [&039;class&039; = &039;form-horizontal&039;],
]);
echo $form-field($model, &039;username&039;);
echo $form-field($model, &039;password&039;)-passwordInput();
echo Html::submitButton(&039;Login&039;, [&039;class&039; = &039;btn btn-primary&039;]);
ActiveForm::end();
8、配置文件
主配置文件 (config/web.php)
php
<?php
$params = require __DIR__ 、&039;/params.php&039;;
$db = require __DIR__ 、&039;/db.php&039;;
$config = [
&039;id&039; = &039;basic&039;,
&039;basePath&039; = dirname(__DIR__),
&039;bootstrap&039; = [&039;log&039;],
&039;aliases&039; = [
&039;@bower&039; = &039;@vendor/bower-asset&039;,
&039;@npm&039; = &039;@vendor/npm-asset&039;,
],
&039;components&039; = [
&039;request&039; = [
&039;cookieValidationKey&039; = &039;your-secret-key&039;,
],
&039;cache&039; = [
&039;class&039; = &039;yii\caching\FileCache&039;,
],
&039;user&039; = [
&039;identityClass&039; = &039;app\models\User&039;,
&039;enableAutoLogin&039; = true,
],
&039;db&039; = $db,
&039;urlManager&039; = [
&039;enablePrettyUrl&039; = true,
&039;showScriptName&039; = false,
],
],
&039;params&039; = $params,
];
return $config;
9、常用辅助类
Html 辅助类
php
use yii\helpers\Html;
echo Html::encode($content);
echo Html::a(&039;Click me&039;, [&039;site/index&039;]);
echo Html::img(&039;@web/images/logo.png&039;);
echo Html::tag(&039;div&039;, &039;Content&039;, [&039;class&039; = &039;container&039;]);
ArrayHelper
php
use yii\helpers\ArrayHelper;
// 从数组中获取值
$value = ArrayHelper::getValue($array, &039;key.subkey&039;);
// 将对象数组转换为键值对
$items = ArrayHelper::map($models, &039;id&039;, &039;name&039;);
// 合并数组
$result = ArrayHelper::merge($array1, $array2);
10、错误处理和日志
配置日志
php
&039;log&039; = [
&039;traceLevel&039; = YII_DEBUG ? 3 : 0,
&039;targets&039; = [
[
&039;class&039; = &039;yii\log\FileTarget&039;,
&039;levels&039; = [&039;error&039;, &039;warning&039;],
],
],
],
记录日志
php
Yii::info(&039;This is an info message&039;);
Yii::error(&039;This is an error message&039;);
Yii::warning(&039;This is a warning message&039;);
这是 Yii2 的基础介绍。如果你有具体的问题或需要深入了解某个特定功能,请告诉我!
📣 商家广告 :
👍
0
👎
IP地址: 196.38.113.198
搜索次数: 7
提问时间: 2025-10-23 07:05:22
🛒 域名购买 :
ltqz.cn 2.js.cn 8.hi.cn jqe.com.cn zhanwai.cn tuosou.cn jgqg.cn qw.gs ousou.cn pjtf.cn
❓️ 热门提问 :
机架机柜区别 vps购买好啦不会使用 ip等级的含义 香港云主机 vps 网上服务器 哪家好 ipmana网络加速器 mysql服务器配置优化 远程访问服务器软件 开发网站软件 转让idc 豌豆Ai站群搜索引擎系统
🌐 域名评估 :
hh.gd.cn kimi.ski wfli.cn hechaocheng.cn si.hl.cn sjvf.cn i-f.com.cn jdwx.cn uvde.cn su8.hn.cn
⛏ 最新挖掘 :
凯里酸汤鱼 贵阳烤脑花 都匀毛尖茶 花溪牛肉粉 贵阳烤生蚝 贵阳烤豆腐 贵阳纸包鱼 贵阳特色菜
🖌 热门作画 :
🤝 关于我们:
豌豆Ai
域名
建站
站群
留痕
推广
评估
源码
开发Ai
工具
日记
价格
加盟
广告
流量
留言
联系
🗨 加入群聊 :
🔗 友情链接 :
商标服务网
北京网站维护
ai提问
🧰 站长工具 :Ai工具 whois查询 搜索
📢 温馨提示 :本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
👉 技术支持 :本站由豌豆Ai 提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。