Yii2-related learning Records, initialize Yii2 (II)-roaming cloud top

Source: Internet
Author: User
Tags email account
Yii2 learning Records, initialize Yii2 (II)-Yii2 has been downloaded before roaming cloud, 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, when others update, they only need to run the init command, and then fill in their own email account to send the mail.

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 implemented it myself. it's just a thought.

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.