![最终产品图片](https://cms-assets.tutsplus.com/uploads/users/317/posts/24994/final_image/feature.jpg)
如果您问“ Yii是什么?” 查阅 Yii Framework简介 ,其中 介绍了Yii 的优点,并概述了Yii 2.0。
在本使用Yii2编程系列中 ,我指导读者使用PHP的Yii2框架。 当您开始使用Yii进行实际开发时,您可能希望使用其高级应用程序模板开始下一个项目。 除其他事项外,它提供了集成的用户管理功能以及两个应用程序,一个用于面向消费者的前端,另一个用于管理后端。
在本教程中,我将向您介绍Yii2高级模板,并指导您进行基本设置和使用。 在使用Yii2进行编程:集成用户注册探索了使用Yii2用户扩展名在基本模板之上实现用户管理的同时,本教程将使用高级模板启动新的存储库,而不是继续我们的基本Yii hello代码库示例。
在开始之前,请记住,我确实会尝试参加以下讨论。 如果您有任何问题或建议,请在下面发表评论,或通过Twitter @reifman与我联系 。 您也可以直接给我发电子邮件 。
如果您注意到本系列中的内容有所延迟,那是因为我最近刚从脑外科手术回来。 感谢您的耐心和支持,很高兴能定期再次写信,我期待继续关注Yii2。
高级模板有何不同
迁移到高级模板的最有用的原因是其实现了用户管理功能,例如注册,登录,注销和密码重置。
高级模板还为大型Web应用程序提供了多个访问树。 它具有面向最终用户和管理员的前端和后端Web应用程序。 但是,这也可以扩展-例如针对主持人或特殊的API,尽管还有其他方法可以将这些功能集成到一个应用程序中。
以下图表显示了基本香草Yii和高级安装之间的主要区别:
![Yii2高级模板与基本模板的比较](https://i-blog.csdnimg.cn/blog_migrate/7d6a836d1118e5ea8fdc876bf79f6f29.png)
在有关Yii2用户扩展的最新教程中,我对它的功能集(作为高级模板的替代品)印象深刻。 但是,它也可以轻松集成到任一安装中。
在开始大型项目之前,有必要探索高级模板。 我将帮助您做到这一点。
安装高级模板
让我们开始使用Yii2安装高级模板。 我们可以按照GitHub上高级模板项目中的说明进行操作。
更新作曲家
首先,我们将确保作曲家拥有所需的软件包:
$ composer global require "fxp/composer-asset-plugin:~1.0.3"
Changed current directory to /Users/Jeff/.composer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
使用高级模板安装Yii
然后,我们可以使用高级项目模板安装Yii。 我们将我们的教程应用程序称为yiiplus :
$ composer create-project --prefer-dist yiisoft/yii2-app-advanced yiiplus
Installing yiisoft/yii2-app-advanced (2.0.6)
- Installing yiisoft/yii2-app-advanced (2.0.6)
Loading from cache
Created project in yiiplus
Loading composer repositories with package information
Installing dependencies (including require-dev)
- Installing yiisoft/yii2-composer (2.0.3)
Loading from cache
- Installing ezyang/htmlpurifier (v4.6.0)
Loading from cache
- Installing cebe/markdown (1.1.0)
Loading from cache
- Installing bower-asset/jquery (2.1.4)
Loading from cache
- Installing bower-asset/jquery.inputmask (3.1.63)
Loading from cache
- Installing bower-asset/punycode (v1.3.2)
Loading from cache
- Installing bower-asset/yii2-pjax (v2.0.4)
Loading from cache
- Installing yiisoft/yii2 (2.0.6)
Loading from cache
- Installing swiftmailer/swiftmailer (v5.4.1)
Loading from cache
- Installing yiisoft/yii2-swiftmailer (2.0.4)
Loading from cache
- Installing yiisoft/yii2-codeception (2.0.4)
Loading from cache
- Installing bower-asset/bootstrap (v3.3.5)
Loading from cache
- Installing yiisoft/yii2-bootstrap (2.0.5)
Loading from cache
- Installing yiisoft/yii2-debug (2.0.5)
Loading from cache
- Installing bower-asset/typeahead.js (v0.10.5)
Loading from cache
- Installing phpspec/php-diff (v1.0.2)
Loading from cache
- Installing yiisoft/yii2-gii (2.0.4)
Loading from cache
- Installing fzaninotto/faker (v1.5.0)
Loading from cache
- Installing yiisoft/yii2-faker (2.0.3)
Loading from cache
Writing lock file
Generating autoload files
初始化我们的Yii应用程序
现在,让我们初始化我们的应用程序:
$ cd ~/Sites/yiiplus
$ php init
Yii Application Initialization Tool v1.0
Which environment do you want the application to be initialized in?
[0] Development
[1] Production
Your choice [0-1, or "q" to quit] 0
Initialize the application under 'Development' environment? [yes|no] yes
Start initialization ...
generate backend/config/main-local.php
generate backend/config/params-local.php
generate backend/web/index-test.php
generate backend/web/index.php
generate common/config/main-local.php
generate common/config/params-local.php
generate console/config/main-local.php
generate console/config/params-local.php
generate frontend/config/main-local.php
generate frontend/config/params-local.php
generate frontend/web/index-test.php
generate frontend/web/index.php
generate yii
generate cookie validation key in backend/config/main-local.php
generate cookie validation key in frontend/config/main-local.php
chmod 0777 backend/runtime
chmod 0777 backend/web/assets
chmod 0777 frontend/runtime
chmod 0777 frontend/web/assets
chmod 0755 yii
chmod 0755 tests/codeception/bin/yii
... initialization completed.
准备数据库
接下来,我将使用MAMP的已安装版本的PHPMyAdmin创建数据库:
![在PHPMyAdmin中创建数据库](https://i-blog.csdnimg.cn/blog_migrate/0d687cddce986c9078e37583a4617590.png)
点击创建 。 在为教程制作屏幕截图时,我忘了单击“创建”,然后想知道为什么我不能迁移我的数据库-它尚不存在。
接下来,编辑/common/config/main-local.php以包括您的数据库设置:
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yiiplus',
'username' => 'root',
'password' => '-localmysqldevpwd-',
'charset' => 'utf8',
],
'mailer' => [
然后,您准备好运行数据库迁移以初始化您的应用程序。 这主要是为用户管理设置表:
$ ./yii migrate
Yii Migration Tool (based on Yii v2.0.6)
Creating migration history table "migration"...Done.
Total 1 new migration to be applied:
m130524_201442_init
Apply the above migration? (yes|no) [no]:yes
*** applying m130524_201442_init
> create table {{%user}} ... done (time: 0.007s)
*** applied m130524_201442_init (time: 0.022s)
Migrated up successfully.
为前端和后端站点配置Apache
当我们使用高级模板配置开发或生产环境时,我们需要将Web服务器指向不同的根路径,实际上是两个。
首先,我们将编辑主机文件以包含frontend.dev和backend.dev:
$ more /etc/hosts
127.0.0.1 localhost
127.0.0.1 frontend.dev
127.0.0.1 backend.dev
在使用MAMP的开发环境中,我将yiiplus目录链接到MAMP的htdocs:
$ cd /Applications/MAMP/htdocs/
$ ln -s ~/Sites/yiiplus /Applications/MAMP/htdocs/yiiplus
然后,我将激活(取消注释)虚拟主机的包含:
$ nano /Applications/MAMP/conf/apache/httpd.conf
# Virtual Hosts
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
并为我的每个服务器名称配置路径:
$ nano /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
NameVirtualHost *:8888
<VirtualHost *:8888>
ServerName frontend.dev
DocumentRoot /Applications/MAMP/htdocs/yiiplus/frontend/web/
<Directory "/Applications/MAMP/htdocs/yiiplus/frontend/web/">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# use index.php as index file
DirectoryIndex index.php
# ...other settings...
</Directory>
</VirtualHost>
<VirtualHost *:8888>
ServerName backend.dev
DocumentRoot /Applications/MAMP/htdocs/yiiplus/backend/web/
<Directory "/Applications/MAMP/htdocs/yiiplus/backend/web/">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# use index.php as index file
DirectoryIndex index.php
# ...other settings...
</Directory>
</VirtualHost>
完成后,这就是前端网站的外观,网址为http://frontend.dev:8888 :
![Yii2高级模板前端主页](https://i-blog.csdnimg.cn/blog_migrate/21c611324a9952aebc31fe2b37d995b4.png)
后端站点将要求您登录-适用于管理员:
![Yii2高级模板后端主页](https://i-blog.csdnimg.cn/blog_migrate/619d7b34187e6c37addad2e660a054f6.png)
探索用户管理
现在,我将向您介绍高级模板的基本用户管理功能。 但是首先,我们需要确保在开发环境中收到来自Yii的电子邮件。
配置电子邮件传递
用户管理发送电子邮件以重置密码,因此我们需要激活Yii的SwiftMailer SMTP配置。 我将使用Mailtrap.io ,我在先前的教程Mailtrap简介:用于应用程序电子邮件的生产前测试的伪SMTP服务器中进行了探讨 。
Mailtrap为您的开发团队提供了一个伪造的SMTP服务器,以测试,查看和共享从预生产环境发送的电子邮件,并使用真实数据进行测试,而不会冒滥发真实客户的风险。 对于许多开发任务,使用Mailtrap是免费的。
本质上,您注册Mailtrap并通过伪造的Mailtrap SMTP服务器发送所有生产前环境的电子邮件。 这是Railsware的Mailtrap的简短视频概述:
如果按照本教程创建一个Mailtrap帐户,您将看到演示收件箱:
![带收件箱的Mailtrap仪表板](https://i-blog.csdnimg.cn/blog_migrate/03bf4d1e2b05c8d5ef0973c845bb612a.png)
而且,当您单击收件箱列表中的“设置”图标时,您会看到每个Mailtrap收件箱都有其自己的SMTP服务器凭据:
![Mailtrap SMTP服务器凭据](https://i-blog.csdnimg.cn/blog_migrate/4668ba6244ee0c372c16fa88e4c1077e.png)
使用Yii,我正在/common/config/main-local.php中更新SwiftMailer SMTP设置。 可能是这样的:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'mailtrap.io',
'username' => '29xxxxxxxxxxx72',
'password' => '2c3xxxxxxxxxxf5',
'port' => '2525',
'encryption' => 'tls',
],
],
请注意,如果您没有如上所示自定义viewPath
,则可能会遇到找不到电子邮件模板文件的错误。
注册和注册
前端注册屏幕如下所示:
![Yii2高级应用程序模板注册屏幕](https://i-blog.csdnimg.cn/blog_migrate/d7631d673863e2a59edf97dadf0d30ab.png)
它会以登录状态将您带入主页:
![Yii2高级应用程序模板已登录](https://cms-assets.tutsplus.com/uploads/users/317/posts/24994/image/signed%20in.jpg)
登入
这是登录屏幕:
![Yii2高级应用程序模板登录](https://i-blog.csdnimg.cn/blog_migrate/034f1a89b5524a468b973c008ab63063.png)
忘记密码了吗
这是“ 忘记密码”屏幕:
![Yii2高级应用程序模板密码重置](https://i-blog.csdnimg.cn/blog_migrate/335a3df8eb3705dc21f4391fd0105e6f.png)
如果您要求输入新密码,可以在Mailtrap中找到它:
![Yii2高级应用程序模板,使用MailTrap for SMTP](https://i-blog.csdnimg.cn/blog_migrate/9e6af6ec80038c1d53aa6d5d605cc631.png)
下一步是什么?
您可能要查看我的使用PHP构建您的启动系列 ,该系列使用Yii2的高级模板。 另外,如果您有兴趣将本教程与Yii2 User集成,请查看有关将Yii2 User与高级模板和Google身份验证 集成的指南 (即将推出)。
希望您喜欢Yai2高级应用程序模板。 我很想听听您是否喜欢基本模板的评论中的反馈。
当我继续深入研究框架的不同方面时,请观看“ 使用Yii2编程”系列中即将发布的教程。 我欢迎功能和主题要求。 您可以在下面的评论中发布它们,或在我的Lookahead Consulting网站上给我发送电子邮件 。
如果您想知道下一个Yii2教程何时到达, 请在Twitter上关注我@reifman或查看我的讲师页面 。 发布后,我的讲师页面将包含本系列中的所有文章。
- Yii2高级模板
- Yii2开发人员交流
- CodeCanyon上基于Yii的脚本的集合