1. Installation
1. Server requirements
The Laravel framework has a few requirements on servers. Of course, Laravel Homestead already meets all these requirements. Therefore, we strongly recommend using Homestead as the local development environment of Laravel.
If you are not using Homestead, make sure that the development environment meets the following requirements:
PHP version> = 5.5.9
PHP extension: OpenSSL
PHP extension: PDO
PHP extension: Mbstring
PHP extension: Tokenizer
2. Install Laravel
Laravel uses Composer to manage dependencies. Therefore, before using Laravel, ensure that Composer is installed on the machine.
Use Laravel installer
First, install Laravel installer through Composer:
Composer global require "laravel/installer"
Ensure ~ /. Composer/vendor/bin is in the system Path. Otherwise, the laravel command cannot be called in any path.
After the installation is complete, use the simple laravel new command to create a new Laravel application in the current directory. For example, laravel new blog will create a new application named blog, all Laravel dependencies are included. This installation method is much faster than using Composer:
Laravel new blog
Use Composer Create-Project
You can also use the create-project command of Composer in the terminal to install the Laravel application:
Composer create-project laravel/laravel -- prefer-dist blog
3. Basic configuration
All configuration files of the Laravel framework are stored in the config directory, and every configuration item has annotations. Therefore, you can browse any configuration file to familiarize yourself with these configuration items.
Directory permission
After installing Laravel, you need to configure read and write permissions for some directories: the storage and bootstrap/cache directories should be writable. If you use the Homestead virtual machine as the development environment, these permissions have been set.
Application Key
The next thing to do is to set the key (APP_KEY) of the application to a random string. If you install the key through the Composer or Laravel installer, the value of the key has passed the key: the generate command is generated. Generally, this string should be 32-bit long and configured through the APP_KEY in the. env file. If you have not renamed the. env. example file to. env, do this now. If the application key is not set, the user Session and other encrypted data may have security risks.
If you want to manually generate the value of this key, use the following Artisan command:
Php artisan key: generate
More configurations
Laravel can be used normally without any other configuration, but you 'd better check config/app again. php file, which contains some configuration that may need to be changed based on the application, such as timezone and locale (used to configure the time zone and localization respectively ).
You may also want to configure other components of Laravel, such as cache, database, and Session. We will discuss these components in subsequent documents.
II. Configuration
1. Introduction
All configuration files of Laravel are stored in the config directory, and each configuration item is annotated to ensure that you can intuitively understand the role and usage of the configuration item when browsing any configuration file.
2. Access configuration value
You can use the global auxiliary function config to access the configuration value anywhere in the application. The configuration value can be a file name + ". + configuration item access. If the configuration item is not configured, the default value is returned:
$ Value = config ('app. timezone ');
If you want to set the configuration value at runtime, pass the array parameter to the config method:
Config (['app. timezone '=> 'America/Chicago']);
3. Environment configuration
Setting different configuration values based on the application running environment can greatly facilitate our development. For example, we usually configure different cache drivers in the local and online environments, this mechanism is easy to implement in Laravel.
Laravel uses the PHP library DotEnv developed by Vance Lucas to implement this mechanism. In the newly installed Laravel, there is. env. example file. If Laravel is installed through Composer, the file has been renamed. env. Otherwise, manually rename the file.
Each time an application accepts a request ,. all configurations listed in env and their values are loaded into the Super global variable $ _ ENV of PHP. Then, you can obtain these configuration values through the auxiliary function env in the application. In fact, if you view the Laravel configuration file, you will find that this helper function is already used in many places:
'Debug' => env ('app _ debug', false ),
The second parameter passed to the env function is the default value. If the environment variable is not configured, the default value is used.
Do not submit the. env file to source code control (svn or git), because each developer/server that uses your application may require different environment configurations.
If you are developing in a team, you need. env. the example file is submitted to the source code control with your application: place some configuration values as placeholders. env. in the example file, other developers will know which environment variables need to be configured to run your application.
Access the current application environment
The current application environment is determined by the APP_ENV variable in the. env file. You can access the value through the environment method of the App facade:
$ Environment = App: environment ();
You can also pass parameters to the environment method to determine whether the current environment matches the given value. If necessary, you can even pass multiple values. If the current environment matches the given value, this method returns true:
If (App: environment ('Local ')){
// The environment is local
}
If (App: environment ('local', 'staging ')){
// The environment is either local OR staging...
}
Application instances can also be accessed through the helper function app:
$ Environment = app ()-> environment ();
4. Configure cache
To accelerate the application, you can use the Artisan command config: cache to cache the configurations of all configuration files to a single file, this will merge all configuration options into a single file so that they can be quickly loaded by the framework.
Once the application is launched, it is necessary to run php artisan config: cache once. However, during local development, you do not need to run this command frequently because the configuration value often needs to be changed.
5. Maintenance mode
When your application is in maintenance mode, all requests to the application will return the same custom view. This mechanism makes it easy to "close" the site when upgrading or maintaining applications. The judgment code for the maintenance mode is located in the default middleware stack of the application. If the application is in the maintenance mode, the HttpException with the status code 503 will be thrown.
To enable the maintenance mode, run the Artisan command down:
Php artisan down
To disable the maintenance mode, the corresponding Artisan command is up:
Php artisan up
Maintenance mode response template
The default maintenance mode response template is resources/views/errors/503. blade. php.
Maintenance Mode & queue
When your site is in maintenance mode, all the queue tasks will not be executed; when the application exits maintenance mode, these tasks will continue to be processed normally.
Alternative maintenance mode
As the command execution in maintenance mode takes several seconds, you can consider using Envoyer to deprecate the command in 0 seconds as an alternative.