Search code examples
phpconstantslumenerror-code

Where should I keep my constants in lumen?


I am writing a web service using Lumen and need to store some constants, specifically error values, but also other configuration parameters. Where would it be most appropriate to put these?


Solution

  • Here is how I did it.

    I made a const.php file within bootstrap folder where I have defined some error codes.

    <?php
    define('VALIDATION_EXCEPTION',422);
    

    I included the const.php file in app.php using require_once.

    <?php
    require_once __DIR__.'./const.php';
    

    Now I can call VALIDATION_EXCEPTION from anywhere. for instance.

    $router->get('/test',function(){
    return VALIDATION_EXCEPTION;
    });
    

    That's it!