Lumen (Laravel) create a custom helper file

1

I have created a file in the path app/Helpers/helper.php where I want to include the helpers I need for my project in lumen, at the moment I have this code

<?php

namespace App\Helpers;
/**
 * Generate a  "random" alpha-numeric string.
 *
 * @param  int  $length
 * @return string
 *
 * @throws \RuntimeException
 */


function randomStr($length = 60)
{
  if (!function_exists('openssl_random_pseudo_bytes')) {
    throw new RuntimeException('OpenSSL extension is required.');
  }

  $bytes = openssl_random_pseudo_bytes($length * 2);

  if ($bytes === false) {
    throw new RuntimeException('Unable to generate random string.');
  }

  return substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $length);
}

In the file composer.json I added the following key

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

And to make the call to the function I do it from a controller in this way

$token = \App\Helpers\randomStr(30);

And the error I get is

  

Call to undefined function App \ Helpers \ randomStr ()

    
asked by ilernet 27.02.2018 в 13:31
source

1 answer

0

In the .json file you are already indicating the helper path, it is no longer necessary to do that in the controller.

Try something like this:

$token = randomStr(30);
    
answered by 28.02.2018 в 04:25