Installation Configuration
Server Requirements
The Larravel framework has certain requirements for PHP version and extension, but these requirements Laravel Homestead All have been met, but if you don't use Homestead (that's a pity), it is necessary to know these things to confirm that your environment meets the requirements:
- PHP >= 7.0.0
- PHP OpenSSL extension
- PHP PDO extension
- PHP Mbstring Extension
- PHP Tokenizer Extension
- PHP XML Extension
After meeting the above requirements, you can start to install Laravel.
Installing Laravel
Larravel uses Composer to manage dependencies. Therefore, before installation, ensure that Composer has been installed on the machine (if not already installed, refer to This document Go and install it).
Via Laravel Installer
First, install the Larravel installer through Composer:
composer global require "laravel/installer"
ensure $HOME/.composer/vendor/bin
In the system path (the corresponding path in Mac is ~/.composer/vendor/bin
, the corresponding path of Windows is ~/AppData/Roaming/Composer/vendor/bin
, where ~
Indicates the current user's home directory), otherwise it cannot be called in any path on the command line laravel
Command.
Once the installation is complete, you can use the simple laravel new
Command to create a new Laravel application in the current directory, for example, laravel new blog
A file named blog
And includes all Larravel dependencies. This installation method is much faster than installing through Composer:
laravel new blog
If you have previously installed an older version of the Larvel installer, you need to update it before installing the latest Larvel 5.5 framework application:
composer global update
Through Composer Create Project
You can also use Composer's create-project
Command to install the Laravel application:
composer create - project --prefer-dist laravel/laravel blog
If you want to download and install other versions of Larave applications, such as version 5.4, you can use this command:
composer create - project --prefer-dist laravel/laravel blog 5.4.*
Local development server
If you have installed PHP locally and want to use PHP's built-in development environment server to provide services for applications, you can use the Artisan command serve
:
php artisan serve
This command will start the development environment server locally, so that in the browser http://localhost:8000
Access the app:

Of course, the more powerful choice of local development environment is Homestead and Valet 。
Configure Laravel
Initialize Configuration
Public directory
After installing Larvel, you need to point the document/web root directory of the Web server to the public
Directory, the index.php
As the front-end controller (single entry), all HTTP requests will enter the application through the file.
configuration file
All configuration files of the Larravel framework are stored in config
All configuration items in the directory have comments, so you can easily browse these configuration files to familiarize yourself with all configuration items.
directory right
After installing Larave, you need to configure read and write permissions for some directories: storage
and bootstrap/cache
The directory should be writable to the user specified by the Web server, otherwise the Larravel application will not work properly. 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 application key (APP_KEY) to a random string. If you install it through Composer or Larvel installer, the key value has passed php artisan key:generate
The command is generated.
In general, the string should be 32 bits long .env
In file APP_KEY
Configure, if you haven't already .env.example
File renamed to .env
, do so now. If the application key is not set, the user session and other encrypted data will have security risks!
More configurations
Larave can be used normally without any other configuration. However, you'd better take a look again config/app.php
File, which contains some configurations that may need to be changed based on the application, such as timezone
and locale
(used to configure the time zone and localize, respectively).
You may also want to configure some other components of Laravel, such as cache, database, session, etc. We will discuss these in subsequent documents.
Web server configuration
The configuration of the virtual host (mapping the domain name to the Laravel application directory) is omitted. For details, please refer to This tutorial Of course, we can leave it to Homestead and Valet in the next article. This article only discusses how to beautify the URL to make it more readable.
Apache
Included in the frame public/.htaccess
File supports hiding index.php
If your Larravel application uses Apache as the server, you need to ensure that Apache is enabled first mod_rewrite
Module to support .htaccess
Parsing.
If Laravel comes with .htaccess
The file does not work. Try to replace the contents as follows:
Options +FollowSymLinks RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !- d RewriteCond %{REQUEST_FILENAME} !- f RewriteRule ^ index.php [L]
Nginx
If you use Nginx, you can use the following site configuration instructions to support URL beautification:
location / { try_files $uri $uri / /index.php? $query _string; }
Of course, if you use Homestead or Valet, the above configuration has been configured for you, and no additional operation is required.
Environment configuration
Setting different configuration values based on the application running environment can bring great convenience to our development. For example, we usually configure different cache drivers in the local and online environments, which is easy to implement in Laravel.
Larave uses PHP extension libraries DotEnv To realize this function, in the newly installed Laravel, there is a .env.example
If Laravel is installed through Composer, the file has been renamed to .env
Otherwise, you need to rename the file manually.
Don't try to .env
When files are submitted to a version control system (such as Git or Svn), on the one hand, the configuration values of the development environment and the online environment are different, so the submission is meaningless. More importantly, .env
It contains a lot of application sensitive information, such as database user name and password. If you accidentally submit the code to Github public warehouse, the consequences will be unimaginable!
If you are developing in a team, you need to .env.example
The file is submitted to the source code control along with your application code: place some configuration values as placeholders in the .env.example
In the file, other developers will know which environment variables need to be configured to run your application.
You can also create a .env.testing
File, which will run the PHPUnit test or execute the --env=testing
Option when overriding the Artisan command from the .env
The value read by the file.
Get the environment variable configuration value
Every time the app accepts a request, .env
All configurations listed in and their corresponding values will be loaded into PHP super global variables $_ENV
Then you can use auxiliary functions env
To get these configuration values. In fact, if you look at Larave's configuration file, you will find that this auxiliary function has been used in many places:
'debug' => env ( 'APP_DEBUG' , false) ,
Pass to env
The second parameter of the function is the default value, which will be used if the environment variable is not configured.
Judge the current application environment
The current application environment is composed of .env
In file APP_ENV
Variable, you can use App
Facade On environment
Method to access its value:
$environment = App::environment();
You can also send environment
Method to pass parameters 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, the method returns true
:
if ( App :: environment ( 'local' )) { // The environment is local } if ( App :: environment ( 'local' , 'staging' )) { // The environment is either local OR staging... }
Access configuration values
You can use global auxiliary functions config
Access the configuration value anywhere in the application code File name+"."+configuration item The default value is returned when the configuration item is not configured:
$value = config( 'app.timezone' );
If you want to set the configuration value at runtime, pass the array parameter to config
Method:
config([ 'app .timezone' => 'Asia / Shanghai' ]);
Cache Profile
To speed up the application, you can use the Artisan command config:cache
Cache the configuration of all configuration files into a single file, which will merge all configuration options into a single file and be quickly loaded by the framework.
Every time the application goes online, it needs to run once php artisan config:cache
However, it is unnecessary to run this command frequently during local development, because the configuration values often change.
Note: If it is executed during deployment config:cache
Command, you need to ensure that only the env
method.
maintenance mode
When your application is in maintenance mode, all requests for the application should return to the same custom view. This function makes it easy to "close" the site when upgrading or maintaining applications. The judgment code for maintenance mode is located in the application default middleware stack. If the application is in maintenance mode, the status code when accessing the application is five hundred and three
Of MaintenanceModeException
Will be thrown.
To turn on maintenance mode and shut down the site, simply execute the Artisan command down
That is:
php artisan down
Can also provide message
and retry
Options for down
Command. message
The value of is used to display or log custom messages, and retry
The value of is used to set the Retry-After
:
php artisan down --message= "Upgrading Database" --retry= sixty
To turn off the maintenance mode and start the site, the corresponding Artisan command is up
:
php artisan up
Note: You can customize the default maintenance mode template by defining your own template. The customized template view is located in resources/views/errors/503.blade.php
。
Maintenance Mode&Queue
When your site is in maintenance mode, all Queue Task Will not be executed; These tasks will continue to be processed normally when the application exits maintenance mode.
Alternatives to maintenance mode
Since the execution of the maintenance mode command takes several seconds, you can consider using Envoyer Realize offline in 0 seconds as an alternative.