scott
scott

Reputation: 3202

Lumen 5.4 Helper class not available globally

I have created Helper function in

lumen\app\Helpers\Helpers.php

and added

<?php
if(!function_exists('config_path'))
{
        /**
        * Return the path to config files
        * @param null $path
        * @return string
        */
        function config_path($path=null)
        {
                return app()->getConfigurationPath(rtrim($path, ".php"));
        }
}

in compoer.json

,
    "files": [
        "app/Helpers/Helpers.php"
    ],

then ran

composer dump-autoload -o
composer dump-autoload 

after that if try to call in web.php

$app->get('/', function () use ($app) {


  echo config_path();


});

Getting following error

Fatal error: Call to undefined function config_path() in D:\xampp\htdocs\lumen\routes\web.php on line 17 enter image description here

Upvotes: 1

Views: 1259

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163758

Looks like web.php is executed before Helpers.php. The best thing you can do is to put any logic into the Controller, Models etc and keep routes only in web.php.

If you don't care about this, you can require() helpers file in index.php, but again, it's a bad practice.

Upvotes: 1

Related Questions