Home  >  Article  >  PHP Framework  >  Share two tips for PHP’s laravel framework

Share two tips for PHP’s laravel framework

silencement
silencementforward
2019-11-25 15:28:382935browse

Share two tips for PHP’s laravel framework

I have used Laravel as a PHP development framework for a long time, but there are some places that are not covered in the official documents, and I forget them every once in a while. I did some simple organizing recently and took notes on it.

1. Route::controller Route naming:

Using Route::controller can reduce a lot of work in route customization, but sometimes it is necessary to name a specific route for use, but The Route::controller method batch-specifies the routes for all methods in a Controller. How should this be named? 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:

// 该函数的签名:
public function controller($uri, $controller, $names = array())
 
// 不命名一般使用:
Route::controller('admin', 'AdminController');
 
// 需要对其中的部分方法命名的话:
Route::controller('admin', 'AdminController', array(
    'getIndex' => 'admin.index',
    'getLogin' => 'admin.login',
    'postLogin' => 'admin.login'
  ));

Recommended to study "laravel video tutorial"

2. Determine the current operating environment based on system variables

The system's default method of determining whether it is a local environment is to specify a set of host names as the local environment in the 'local' array. For example, if you are developing on an office computer or Macbook, you need to use the two host names. I think it’s very troublesome to write everything down. It was changed to judge based on $_SERVER['LARAVEL_ENV'], so that I can define the environment variable of 'LARAVEL_ENV' in all development machines with the value of 'local', so the development machine will automatically recognize it as the 'local' environment. , and 'production' in other cases.

The code is as follows:

// 默认的写法是根据主机名判断是否本地环境
$env = $app->detectEnvironment(array(
    'local' => array('homestead');
));
// 修改为先判断系统变量是否指定,没有才判断主机名
$env = $app->detectEnvironment(function(){
    $_env = getenv('LARAVEL_ENV') ? getenv('LARAVEL_ENV') : array(
      'local' => array('homestead')
    );
    return $_env;
});
// 这实际是去读取 $_SERVER['LARAVEL_ENV'] 的值
// 在 Apache 中,可以用 SetEnv 设置,
// 在 Nginx 中,可以用 fastcgi_param 设置

The above is the detailed content of Share two tips for PHP’s laravel framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:www.word666.com. If there is any infringement, please contact admin@php.cn delete