php - Laravel custom helper - undefined index SERVER_NAME -
in laravel 5.1, created custom helper file: custom.php
load in composer.json
:
"autoload": { "files": [ "app/helpers/custom.php" ] },
and contains method:
function website() { return str_replace('dashboard.', '', $_server['server_name']); }
it works expected, every time php artisan
commands, call stack , message:
notice: undefined index: server_name in /path/to/custom.php on line 4
why so? method returns correct value when run within laravel app.
$_server['server_name'] global variable accessible when running application through browser. through error when run application through php-cli/through terminal. change code to
function website() { if(php_sapi_name() === 'cli' or defined('stdin')){ // section of code runs when application being runned terminal return "some default server name or can use environment set server name" }else{ // section of code run when app being run browser return str_replace('dashboard.', '', $_server['server_name']); } }
hope helps you.
Comments
Post a Comment