I wrote a "Helper" controller that I want to use in other controllers, but I am doing something wrong. Is Lumen Service what i need? Then I just don't get how to set it up.
My main class:
namespace App\Http\Controllers;
use App\Http\Controllers\HelperController as Helper;
class InitController extends Controller
{
public function work($hash, $type)
{
return response()->json([
'answer' => Helper::makeCodeUrl()
]);
}
}
Helper controller:
namespace App\Http\Controllers;
class HelperController extends Controller
{
public function makeCodeUrl($arr, $type){
return str_random(32);
}
}
Remember, a Controller is responsible for handling Routing Logic. With this in mind, there's really no such thing as a "Helper Controller", as no routes are being mapped to it and therefore isn't really a "Controller" in the traditional sense.
What you're describing is what's called a "Service Class". Any application of reasonable complexity will use service classes as a means to abstract away all of the business logic from Controllers into reusable components.
In your case, a Service Class is exactly what you need. In order to build your own, you need to do a few things:
composer dump-autoload
As a quick proof of concept, this is what it will end up looking like:
bootstrap/app.php
app/Providers/HelperServiceProvider.php
app/Services\Helpers\HelperService.php
InitController.php
While I can appreciate that there's a bit of a learning curve with this particular pattern of code, I would highly recommend reading as much as possible about Service Providers. It will prove to become extremely valuable in the future, and will enable you to bootstrap entire 3rd party libraries into Laravel or Lumen installations.