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:
- Register a ServiceProvider
- Add the actual Service provider class, and bind your Helper Service to the application's IOC container
- Create your Helper Service
- Typehint the Helper Service in your Controller's Constructor
- Run
composer dump-autoload
As a quick proof of concept, this is what it will end up looking like:
bootstrap/app.php
/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/
$app->register(App\Providers\HelperServiceProvider::class);
app/Providers/HelperServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\Helpers\HelperService;
class HelperServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(HelperService::class, function(){
return new HelperService;
});
}
}
app/Services\Helpers\HelperService.php
namespace App\Services\Helpers;
class HelperService
{
public function makeCodeUrl(){
return str_random(32);
}
}
InitController.php
namespace App\Http\Controllers;
use App\Services\Helpers\HelperService;
class InitController extends Controller
{
protected $helperService;
public function __construct(HelperService $helperService)
{
$this->helperService = $helperService;
}
public function work($hash, $type)
{
return response()->json([
'answer' => $this->helperService->makeCodeUrl()
]);
}
}
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.