Yii2的相关学习记录,初始化Yii2(二),初始化yii2
Yii2的相关学习记录,初始化Yii2(二),初始化yii2
前面已经将Yii2下载下来了,那我们就需要能实际的使用。
一、初始化,因为我都是在windows系统下,所以用cmd命令打开下载下来的Yii2的根目录。然后运行下面命令:
init
会提示选择0为开发环境,1为生成环境。一般选生产环境。后期可以同样输入此命令来切换开发环境和生产环节,但是需要注意切换时需要提前在“environments”目录中中提前写好生产环境的配置,以免切换时覆盖丢失配置项,后面还会说,现在继续往下走。
二、配置数据库,现在前台访问地址为:http://localhost/vishun/backend/web/index.php,后台访问地址为:http://localhost/vishun/frontend/web/index.php,其中vishun是我Yii框架安装的目录名称。现在访问时会报数据库连接的错误,因为数据库没有连接上,所以,我们需要先创建一个数据库(个人比较喜欢用phpmyadmin),然后在Yii2中配置数据库参数:
配置文件在三个地方存在,common/config,frontend/config,backend/config,一般来说common是放前后台都会用到的配置文件,而frontend和backend则是放各自的配置文件,如果重复,则前后台的会覆盖公用的。
每个文件夹下又分为main.php和main-local.php,这主要是为了团队协作,一般团队成员都在自己的电脑环境下开发,然后提交到git或svn上。所以团队成员用自己的数据库账号密码什么的,如果提交上去,其它团队成员更新下来和自己的账号密码不对,就会产生错误,所以数据库等配置文件放在*-local文件中,提交时*-local的文件不提交。
但是如果不提交还是会有问题,像是一个新成员加入,直接从git上下载代码下来,但是数据库配置文件由于所有人都没有提交,整个程序不完整,那就导致新成员根本就没有数据库的配置。所以这里还需要用到上方提到过的environments目录,这里面有dev(开发)和prod(生产)两种环境,主要是存储*-local文件的模板。在使用init命令时,就会根据这里面的文件来生成*-local文件,然后新成员在文件中填上自己的数据库账号密码就可以使用了。
以上团队环境详细说明可以看这里:深入理解Yii2.0--环境和配置文件,说的非常详细。
三、配置完成后就可以,还是会报错,但不是报数据链接的错误了,而是user表没有找到。那我们就创建user表。Yii2高级版框架中user表已经写好了,可以打开console/migrations下有*_init.php文件,这个就是写好的user表。只需在cmd中Yii2根目录下运行命令:
yii migrate
这样user表就创建好了。这时访问前后台都OK了。migrate命令是Yii框架迁移或者修改数据库的工具,尤其的在团队协作中,如果成员在本地电脑上增加了一张表,如何告知其它成员呢,用yii migrate/create命令就可在console/migrations创建一个改动,然后就增加的表内容写在里面,提交后其它成员更新后,用yii migrate命令,即可将自己的数据库同步到最新的,绝对是十分方便。migrate功能还很多,自己也没完全弄清楚呢,可能在下面的前后台分离章节中还会说。
四、顺便说下配置邮箱吧。完成前三步功能时,就可以注册个会员看看,当忘记密码时,则在登录界面点击忘记密码,需要通过邮件来找回,所以我们需要配置下邮箱:
Yii2高级版框架中是集成了邮箱类的,在common/config/main-local.php文件可以看到:
'mailer' =><span> [ </span>'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@common/mail',<span> ]</span>,
这就是相关的配置文件,之所以放到*-local文件中,因为包含个人账号的敏感信息。
上面的配置信息是不全的,需要填写发件邮箱,端口号什么的,所以修改上面为:
'mailer' =><span> [ </span>'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@common/mail', <span>//</span><span> send all mails to a file by default. You have to set // 'useFileTransport' to false and configure a transport // for the mailer to send real emails.</span> 'useFileTransport' => <span>false</span>, 'transport' =><span> [ </span>'class' => 'Swift_SmtpTransport', 'host' => 'smtp.163.com', <span>//</span><span>我这里用163邮箱 </span> 'username' => '你的邮箱名', 'password' => '你的邮箱密码', 'port' => '25', 'encryption' => 'tls',<span> ]</span>,
<code class="hljs dart"><span class="hljs-string"> //'messageConfig'=>[ // <span class="hljs-string">'charset'=><span class="hljs-string">'UTF-8', // <span class="hljs-string">'from'=>[<span class="hljs-string">'你的邮箱名'=><span class="hljs-string">'robot'] //], </span></span></span></span></span></span></code><span> ]</span>,
而发送邮件的整个流程方法在Yii2高级版中都写好了,所以只需要配置就能发送了。
配置完上方后,可能找回密码发送邮件是应该还会报错,类似这种:
Expected response code 250 but got code "553", with message "553 Mail from must equal authorized user
这是因为有些邮件服务器要求from和username必须一直,例如网易的服务器,而在frontend/models/PasswordResetRequestForm.php中的sendEmail方法规定在63行左右,
->setFrom([\Yii::<span>$app</span>->params['supportEmail'] => \Yii::<span>$app</span>->name . ' robot'])
From是取得supportEmail参数,这个参数在common/config/params.php中定义了默认为:admin@example.com,配置文件中的邮箱名和这个不符合所以报错。方法一是把这个参数改成‘你的邮箱名’就可以正常发送了;方法二是将上方配置文件<span class="hljs-string">messageConfig</span>
注释的取消,然后->setFrom这行删除掉。(所有的这些首先确保先开通smtp服务)
顺便说下,因为你邮箱配置实在*-local文件中,所以为了其它成员也都能用你这个配置,应该在environments/dev/common/main-local.php中添加:
'mailer' =><span> [ </span>'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@common/mail', <span>//</span><span> send all mails to a file by default. You have to set // 'useFileTransport' to false and configure a transport // for the mailer to send real emails.</span> 'useFileTransport' => <span>false</span>, 'transport' =><span> [ </span>'class' => 'Swift_SmtpTransport', 'host' => '', <span>//</span><span>空着,让其他人自己填写</span> 'username' => '', 'password' => '', 'port' => '', 'encryption' => 'tls',<span> ]</span>, 'messageConfig'=><span>[ </span>'charset'=>'UTF-8', 'from'=>[''=>'robot'<span>] ]</span>,<span> ]</span>,
这样,其他人只需要填上自己的邮箱账号就可以发送邮件了。
很多时候邮件填写都是从数据库获取的,这时候就不应该在配置文件中了,而是应该单独写个类,(类似新建components文件并引入,写在这里面),顺便封装下发送方法。我自己也没实习过,只是思路而已。
以上就是Yii2高级版的初始化,下节可能记录下如何套用好看的后台界面和gii自定义模板什么的。睡觉了先。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When Windows detects that a device's driver is incompatible or corrupt, it cannot initialize it. The device stops working and when the user investigates, a "Windows cannot initialize the device driver for this hardware (Code 37)" error message appears in the Device Status pane. Solving this problem is easy and you can do it easily. Fix 1 – Scan your computer You can scan your device for any hardware changes. Windows will eventually check and detect any driver changes. 1. You just need to right-click on the "Windows Key" and click on "Device Manager". 2. When Device Manager appears on your system, click Actions on the menu bar. 3. Here,

Error code 37 means that Windows has detected a problem with the device driver and cannot start the hardware device. This means that the software device driver is not installed correctly on your system, or the hardware you are trying to use is not compatible with Windows. You already know that drivers are crucial for the smooth functioning of your device, so if you are going to use a hardware device, you need to resolve this error immediately. In this article, we will show you various ways to bypass this error. What causes Windows error code 37? When you get Windows cannot initialize the device driver for this hardware. (Code 37) message, it can be caused by several issues, including: Compatibility Issues – When you add a hard

Solution: 1. Reinstall the application; 2. Repair or reinstall the DLL; 3. System restore or checkpoint recovery; 4. Scan using System File Checker (SFC); 5. Check startup items and services; 6. Use Tools; 7. Check official documentation or forums; 8. Consider security software; 9. Check the event viewer; 10. Seek expert help, etc.

The win7 system is a very excellent high-performance system. During the continuous use of win7, many friends are asking how to initialize the computer in win7! Today, the editor will bring you how to restore the factory settings of a win7 computer. Related information on how to initialize the computer in win7: Detailed instructions with pictures and text. Steps: 1. Open the "Start Menu" and enter. 2. Click to enter the settings at the bottom of the left side. 3. In the Win10 update and recovery settings interface, select. 4. Click below "Remove all content and reinstall Windows". 5. You can see the following "Initialization" settings, and then click. 6. Enter the "Your computer has multiple drives" setting option. There are two options here, you can choose according to the situation.

Many gamers have encountered the frustrating issue of the game failing to initialize the graphics system. This article will delve into the common reasons behind this problem and find simple yet effective solutions that will get you back on the board and beating the level in no time. So, if you are getting Unable to initialize graphics system error message in Rollercoaster Tycoon, Assassin’s Creed, Tony Hawk’s Pro Skater, etc., then follow the solutions mentioned in this article. Initialization error Unable to initialize the graphics system. Graphics cards are not supported. Fix the Unable to initialize the graphics system error message To resolve the Unable to initialize the graphics system error in games like Rollercoaster Tycoon, Assassin's Creed, Tony Hawk's Pro Skater, etc., you can try the following workarounds: Update your graphics card driver in Compatibility Mode

The win7 system is a very excellent high-performance system. Recently, many friends of the win7 system are looking for how to initialize the network settings in win7. Today, the editor will bring you the details of win7 computer network initialization. Let’s take a look at the tutorial. Detailed tutorial on how to initialize network settings in win7: Graphical steps: 1. Click the "Start" menu, find and open the "Control Panel", and then click "Network and Sharing Center". 2. Then find and click "Change Adapter Device". 3. Next, in the window that opens, right-click "Local Area Connection" and then click "Properties". 4. After opening it, find "Internet Protocol Version (TCP/IPv4)" and double

C++ differs from Python in terms of a dictionary with the same name, but it has the same data structure with similar functionality. C++ supports mapping, which can be used in the STL class std::map. The map object contains a pair of values in each entry, one is the key value and the other is the map value. Key values are used to search for and uniquely identify entries in the map. While mapped values are not necessarily unique, key values must always be unique in the map. Let's take a look at how to use mapping. First, let's see how to define a mapped data structure in C++. Syntax #includemap<data_type1,data_type2>myMap; Let’s take an example to see how to do this − Example #incl

SSDs have revolutionized the world of technology. These data storage solutions, with their ultra-fast and unparalleled read/write speeds, make every user eager to install their system operating system in them. But what if your new SSD doesn’t show up on your Windows device? This is a typical issue you might face with a brand new SSD plugged into your system. Don't worry. We just got the right solution, all you have to do is follow these steps and your SSD will be back in your File Explorer in no time. Solution - 1. Check the SSD connection. If you are using a USB hub, connect the SSD directly to your system. Check the SSD's connectors for any physical defects.
