1
votes

I'm trying to create a Helper class using the Lumen Framework, but it's not working.

Files Structure

composer.json ./composer.json

    [...]
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/",
            "App\\Libraries\\": "app/Libraries"
        }
    },
    [...]

Controller that is calling to the Helper Class ./app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use \Libraries\Helpers;

class UserController extends Controller {

  /**
   * Show a list of all of the application's users.
   *
   * @return Response
   */
  public function index() {
    return Helpers::test();
  }
}

Helper Class ./app/Libraries/Helpers.php

<?php

namespace App\Libraries;

class Helpers {
  public function test() {
    return "test";
  }
}

Calling the UserController@index ./routes/web.php

    [...]
    $router->get('/', 'UserController@index');
    [...]

Error in route

Whoops, looks like something went wrong.

I'm trying everything but the Helper class are not working.

1
Additional to the given answer below... You're also trying to call a non-static method static. Instead of public function test() { define your method as static (public static function test() {) when you want to call this method as staticCodyKL

1 Answers

2
votes

You forgot to add App in front of your namespace in the UserController.

use App\Libraries\Helpers;

Also, there is no need to add the extra autoload rule in your composer.json. app/libraries is already in the first rule. All subfolders are always included.