Create custom config in Lumen

mAm
mAm
Jan 1 · 1 min read

Follow these 3 steps to do this.

  1. Open your .env file and add your configuration, for an example PAGINATION_LIMIT=100 (we can access this value via env() helper function, but it is always recommended to access your configuration via config() helper function)
  2. Create config directory in your project’s root directory
  3. Create your configuration file inside config directory, for an example product.php
  4. Your configuration files always lives in ROOT_DIR/config/
  5. Add your configuration in product.php like the following
<?php
/**
*
@category Product-service
*
@package Product
*
@author mAm <mamreezaa@gmail.com>
*/

return [

/*
|--------------------------------------------------------------------------
| Pagination
|--------------------------------------------------------------------------
|
| This value gives, the maximum number of records a request can return
|
*/
'pagination' => env('PAGINATION_LIMIT', 100),

];

6. If you look at this file, it only return an array of configurations, you can add more configuration like you add more elements to an array.

7. Now we need to inform Lumen, that we have a new configuration file that it has to load when the application stat to run. to do that open your bootstrap/app.php file and add $app->configure(product) after variable $app initiated.

DONE

We can get this configuration using config() helper function.

Ex: config('product.pagination')

mAm

Written by

mAm

Student in web development. mostly learning PHP, Magento2, ORO, Symfony and Lumen