Yii2 install and simple to use

Some time ago the first time Yii2 framework, encountered some problems, recorded here.

Yii2 Installation: Installation by composer

1, we must first install the composer, I covered in another blog post on how to install under Windows composer.

2, Global Settings Composer uses Chinese Mirror:

composer config -g repo.packagist composer https://packagist.phpcomposer.com

3, composer-asset-plugin installed (if not installed behind composer-asset-plugin installation problems yii2, the default here to install the latest version)

composer global require "fxp/composer-asset-plugin"

4, (1) the installed base version, projectName own project name.

composer create-project --prefer-dist yiisoft/yii2-app-basic projectName

(2) Premium Edition installed. (Advanced Edition distinguishes between front and back office directory backend, frontend)

composer create-project --prefer-dist yiisoft/yii2-app-advanced projectName

Note: Premium after the installation needs to be initialized, "init.bat" Double-click the file in the root directory, select the "0" (indicating development environment), then select "yes" on it, it will automatically generate a file entry.

5, configure the domain name information can be visited. (Advanced Edition need to configure domain name front and back, respectively)

possible problems:

Problems may occur after you install down: Can not find vendor folder.

The reason: composer-asset-plugin is not installed or the version is too low

The solution: find the root of the file, that composer.json file directory, open the command line: composer update

Yii 2 is simple to use:

1, route optimization

Find /frontend/config/main.php file, locate the following code, remove the comments, after running discovery can be removed "? R =" a "/" instead, but still can not get rid of index.php.

'urlManager' => [
    'enablePrettyUrl' => to true, landscaping // url removed? r =
    'showScriptName' => false, // hide index.php
    'rules' => [
    ],
],

To remove the index.php, .htaccess files also need to add the entry file, as follows:

# 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

Note: The front and rear end are required to configure it again.

2, hump naming path access

If used in yii2 camelCase, the default "-" To access method in UserAdminController actionTestUser controller, the access path is: / user-admin / test-user

3, using the post method to access

In Yii2 If not treated, the use of post access error. This is because Yii2 use post will default CSRF verification request.

Here can be added to validate or cancel the background can form CSRF verification.

Method a: :( verification added in the form if this method is best)

<input name="_csrf" type="hidden" id="_csrf" value="<?= Yii::$app->request->csrfToken ?>" />

Method two: Global added 'enableCsrfValidation' in the configuration file => false

Found "request" configuration the "components" in the config folder configuration, in which addition of 'enableCsrfValidation' => false indicates cancel CSRF verification.

Method three: partially disposed, arranged in a single controller

The code may be added at a single controller:

public $enableCsrfValidation = false;

Thus this method can be used to control the post request.

Yii2 deal with encryption and password verification

In Yii2 provided in a series of password encryption and authentication methods to facilitate our use, it uses bcrypt algorithm. View source we can find it using the PHP function password_hash () and crypt () is generated.

encryption:

/ **
 * $ password password to encrypt
 * $ hash encrypted hash string
 * /
$ hash = Yii :: $ APP-> getSecurity () -> generatePasswordHash ($ password);

verify password:

/ **
 * $ password to verify the cleartext password
 * $ hash encrypted hash string
 * /
Yii :: $ APP-> getSecurity () -> the validatePassword ($ password, $ hash);

Guess you like

Origin www.linuxidc.com/Linux/2019-06/158902.htm