Home > Article > Backend Development > Yii2 related learning records, initializing Yii2 (2), initializing yii2_PHP tutorial
We have downloaded Yii2 before, then we need to be able to actually use it.
1. Initialization, because I am under a windows system, I use the cmd command to open the root directory of the downloaded Yii2. Then run the following command:
init
You will be prompted to select 0 as the development environment and 1 as the production environment. Generally choose the production environment. You can also enter this command later to switch the development environment and the production link. However, you need to note that when switching, you need to write the configuration of the production environment in advance in the "environments" directory to avoid overwriting and losing configuration items during the switch. I will talk about it later. Now Keep going down.
2. Configure the database. The front-end access address is now: http://localhost/vishun/backend/web/index.php, and the back-end access address is: http://localhost/vishun/frontend/web/index.php , where vishun is the directory name of my Yii framework installation. Now when accessing, a database connection error will be reported because the database is not connected, so we need to create a database first (I personally prefer to use phpmyadmin), and then configure the database parameters in Yii2:
Configuration files exist in three places, common/config, frontend/config, and backend/config. Generally speaking, common is a configuration file used in both front and backends, while frontend and backend are used to store their own configuration files. If repeated, the front and back ones will overwrite the common ones.
Each folder is divided into main.php and main-local.php. This is mainly for team collaboration. Generally, team members develop in their own computer environment and then submit it to git or svn. Therefore, team members use their own database account and password. If they submit it and other team members update it and their own account and password are incorrect, an error will occur. Therefore, the database and other configuration files are placed in the *-local file. When submitting, *- Local files are not submitted.
But if you don’t submit, there will still be problems. For example, if a new member joins, the code will be downloaded directly from git. However, since no one has submitted the database configuration file, the entire program will be incomplete, which will cause the new member to fail at all. There is no database configuration. Therefore, we also need to use the environments directory mentioned above. There are two environments: dev (development) and prod (production), which mainly store templates for *-local files. When using the init command, the *-local file will be generated based on the files in it, and then the new member can use it by filling in his or her database account password in the file.
The detailed description of the above team environment can be found here: In-depth understanding of Yii2.0--environment and configuration files, which is very detailed.
3. After the configuration is completed, it will be fine. An error will still be reported, but it is not a data link error, but the user table is not found. Then we create the user table. The user table in the Yii2 Advanced Edition framework has been written. You can open the *_init.php file under console/migrations. This is the written user table. Just run the command in the Yii2 root directory in cmd:
yii migrate
In this way, the user table is created. At this time, access to both the front and backends is OK. The migrate command is a tool for migrating or modifying databases in the Yii framework. Especially in team collaboration, if a member adds a table on the local computer, how to inform other members? Use the yii migrate/create command to create it in console/migrations Make a change, and then write the added table content in it. After submission, after other members are updated, you can use the yii migrate command to synchronize your database to the latest one, which is absolutely very convenient. There are many functions of migrate, and I haven’t fully understood them yet. They may be discussed in the chapter on front-end and back-end separation below.
4. By the way, let’s configure the email address. After completing the first three steps, you can register as a member and take a look. When you forget your password, click Forgot Password on the login interface. You need to retrieve it through email, so we need to configure the email address:
The Yii2 Advanced Edition framework integrates the mailbox class, which can be seen in the common/config/main-local.php file:
'mailer' =><span> [ </span>'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@common/mail',<span> ]</span>,
This is the relevant configuration file. It is placed in the *-local file because it contains sensitive information about personal accounts.
The above configuration information is incomplete. You need to fill in the sending email address, port number, etc., so modify the above to:
'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>,
The entire process of sending emails has been written in Yii2 Advanced Edition, so you only need to configure it to send.
After configuring the above, you may still get an error when retrieving your password and sending an email, similar to this:
Expected response code 250 but got code "553", with message "553 Mail from must equal authorized user
This is because some mail servers require from and username to be consistent, such as NetEase's server, and the sendEmail method in frontend/models/PasswordResetRequestForm.php is specified at around line 63,
->setFrom([\Yii::<span>$app</span>->params['supportEmail'] => \Yii::<span>$app</span>->name . ' robot'])
From is to obtain the supportEmail parameter. This parameter is defined in common/config/params.php and the default is: admin@example.com. The email name in the configuration file does not match this, so an error is reported. The first method is to change this parameter to 'your email name' and you can send it normally; the second method is to cancel the <span class="hljs-string">messageConfig</span>
comment in the above configuration file, and then delete the line ->setFrom. (For all of this, make sure you activate the smtp service first)
顺便说下,因为你邮箱配置实在*-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自定义模板什么的。睡觉了先。