Yii2-related learning records, initialize Yii2 (II), and initialize yii2

Source: Internet
Author: User
Tags email account

Yii2-related learning records, initialize Yii2 (II), and initialize yii2

We have downloaded Yii2 before, so we need to be able to actually use it.

I. Initialization. Because I am in windows, run the cmd command to open the downloaded Yii2 root directory. Run the following command:

init

A prompt is displayed, indicating that 0 is the development environment and 1 is the generation environment. Generally, select the production environment. You can also enter this command later to switch the development environment and production processes. However, you must write the production environment configuration in the "environments" directory in advance during the switchover, to avoid overwrite and loss of configuration items during the switchover, we will continue later.

2. Configure the database. The current front-end access address is: http: // localhost/vishun/backend/web/index. php. the backend access address is http: // localhost/vishun/frontend/web/index. php, where vishun is the directory name installed by my Yii framework. Currently, a database connection error is reported during access. Because the database is not connected, you need to create a database (phpmyadmin is preferred) and configure the database parameters in Yii2:

The configuration file exists in three places: common/config, frontend/config, and backend/config. In general, common is the configuration file that will be used in the frontend and backend, frontend and backend put their respective configuration files. If they are repeated, the frontend and backend will overwrite the public ones.

Each folder is divided into main. php and main-local.php, which is mainly for teamwork, General team members are developed in their own computer environment, and then submitted to git or svn. Therefore, if the team members use their own database account and password or something, if they submit the information and the other team members update the information and their account and password are incorrect, an error will occur, therefore, the configuration files such as databases are placed in the *-local file, and the *-local file is not submitted at the time of submission.

However, if you do not submit the file, it will still be a problem. For example, if you add a new member and download the Code directly from git, but the database configuration file is incomplete because no one has submitted the file, as a result, no database configuration is available for new members. Therefore, we need to use the environments directory mentioned above. There are two environments, dev (development) and prod (production), which are mainly templates for storing *-local files. When using the init command, the *-local file will be generated based on the above file, and then the new Members can fill in their own database account password in the file.

For details about the above team environment, refer to the following link: an in-depth understanding of Yii2.0 -- Environment and configuration file, which is very detailed.

3. After the configuration is complete, an error will still be reported, but not the Data Link error, but the user table is not found. Create a user table. In the Yii2 advanced edition framework, the user table has been written. You can open the * _ init. php file in the console/migrations. This is the written user table. Run the following command in the Yii2 directory in cmd:

yii migrate

In this way, the user table is created. At this time, the frontend and backend are all 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 to the local computer, how can he tell other members, you can use the yii migrate/create command to create a change in console/migrations, and then write the added table content in it. After submitting the change, other members will use the yii migrate command, it is absolutely convenient to synchronize your database to the latest one. There are still a lot of migrate functions, and I haven't fully figured it out yet. It may be said in the separation of the front and back sections below.

4. Configure the mailbox by the way. When you complete the first three steps, you can register a member. If you forget your password, click forgot password on the logon page. You need to retrieve the password by email. Therefore, you need to configure the mailbox:

Yii2 pro framework is integrated with the mailbox class, you can see in the common/config/main-local.php file:

'mailer' => [    'class' => 'yii\swiftmailer\Mailer',    'viewPath' => '@common/mail',],

This is the related configuration file, which is put into the *-local file because it contains sensitive information of the personal account.

The configuration information above is incomplete. You need to fill in the sender's mailbox, port number or something, so modify the above:

'Mailer' => ['class' => 'yii \ swiftmailer \ mailer ', 'viewpath' =>' @ common/mail ', // 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. 'usefiletransport '=> false, 'Transport' => ['class' => 'SWIFT _ SmtpTransport ', 'host' => 'smtp .163.com ', // here, I use the 163 mailbox 'username' => 'your mailbox name', 'Password' => 'your mailbox password', 'Port' => '25 ', 'encryption '=> 'tls',],
// 'Messageconfig' => [// 'charset' => 'utf-8 ', // 'from' => ['your mailbox name' => 'Robot '] //],],

The whole process for sending emails has been completed in Yii2 advanced edition, so you only need to configure it to send emails.

After the configuration is complete, an error may occur when you retrieve the password and send the email, similar to the following:

Expected response code 250 but got code "553", with message "553 Mail from must equal authorized user

This is because some email servers require the from and username to be consistent, such as Netease's server, while the sendEmail method in frontend/models/PasswordResetRequestForm. php requires about 63 lines,

 ->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])

From is the get supportEmail parameter, which is defined in common/config/params. php as the default: admin@example.com, the mailbox name in the configuration file does not match this so an error is reported. The first method is to change this parameter to 'Your mailbox name' and then send it normally. The second method is to change the configuration file above.messageConfigUncomment, and then-> setFrom deletes the line. (Make sure that the smtp service is activated first)

By the way, because your mailbox configuration is really in the *-local file, so for other members can also use your configuration, you should add in the environments/dev/common/main-local.php:

'Mailer' => ['class' => 'yii \ swiftmailer \ mailer ', 'viewpath' =>' @ common/mail ', // 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. 'usefiletransport '=> false, 'Transport' => ['class' => 'SWIFT _ SmtpTransport ', 'host' => '', // empty, let others fill in 'username' => '', 'Password' =>'', 'Port' => '', 'encryption' => 'tls ',], 'messageconfig' => ['charset' => 'utf-8', 'from' => [''=> 'Robot '],],

In this way, other people only need to fill in their own email account to send emails.

In many cases, the email is obtained from the database, so it should not be in the configuration file, but should be written into a separate class (similar to creating and introducing the components file, written here ), encapsulate the sending method by the way. I have never practiced on my own, but I just thought about it.

The above is the initialization of the Yii2 advanced edition. The next section may record how to apply a nice background interface and gii custom template or something. Go to bed first.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.