Two tips for Laravel in the PHP framework and two tips for laravel. Two tips for Laravel PHP Framework: laravel and Laravel have been using Laravel as the PHP development framework for a long time, but some official documents do not cover it, at regular intervals, there are two tips for the PHP framework Laravel and two tips for laravel.
It has been a long time to use Laravel as a PHP development framework, but some official documents do not cover it and will be forgotten at intervals. Recently, I made a little simple sorting and recorded it as a memo.
1. Route: controller Route name:
Using Route: controller can reduce a lot of work in Route customization, but sometimes you need to name a specific Route for use, but Route :: the controller method specifies the routes of all the methods in the Controller in batches. how should we name them? You can use the third parameter in controller ($ uri, $ controller, $ names = array (). This is an array. the key of the array is the method, and the value of the array is the name.
The code is as follows:
// Signature of the function:
Public function controller ($ uri, $ controller, $ names = array ())
// If it is not named, it is generally used:
Route: controller ('admin', 'admincontroller ');
// If you need to name some of these methods:
Route: controller ('admin', 'admincontroller', array (
'Getindex' => 'admin. Index ',
'Getlogin' => 'admin. login ',
'Postlogin' => 'admin. login'
));
2. determine the current running environment based on system variables
By default, the system determines whether the local environment is located by specifying a group of host names in the 'local' array as the local environment host name, for example, to be developed on both the office machine and Macbook, you need to write both host names. I think this is very troublesome. Changed to judge based on $ _ SERVER ['laravel _ env'], so that I can define the 'laravel _ env' environment variable on all development machines with the value 'local ', as a result, the development machine will automatically identify the environment as 'local', while the other case is 'production '.
The code is as follows:
// The default method is to determine whether the local environment is based on the host name.
$ Env = $ app-> detectEnvironment (array (
'Local' => array ('homestead ');
));
// The host name is determined only when the system variable is specified.
$ Env = $ app-> detectEnvironment (function (){
$ _ Env = getenv ('laravel _ env ')? Getenv ('laravel _ env'): array (
'Local' => array ('homestead ')
);
Return $ _ env;
});
// Read the value of $ _ SERVER ['laravel _ env '].
// In Apache, you can set it with SetEnv,
// In Nginx, you can use fastcgi_param to set
Worker has been using Laravel as the PHP development framework for a long time, but some official documents do not cover it, and every other time...