Create Lumen Packages
This will be a short article on how to create a route inside lumen package. A package might be useful if you want to separate some business logic out of the main code or just want to make it shareable. For initial step please follow https://laravelpackage.com, I was from there but unfortunately my package not working since I’m using Lumen 8.x, if you had same case with me then you can try steps below:
Controllers
use Laravel\Lumen\Routing\Controller as BaseController instead of Illuminate\Routing\Controller
// 'src/Http/Controllers/Controller.php'
<?php
namespace JohnDoe\BlogPackage\Http\Controllers;
use Laravel\Lumen\Routing\Controller as BaseController;class Controller extends BaseController{ }
Routes
You can follow the same pattern to create route:
// 'routes/web.php'
<?php
$router->group(['prefix'=> 'blog'], function() use ($router) {
$router->post('hello', [
'uses' => 'PostController@index'
]);
});
but you need to register route in service provider like below:
// 'src/BlogPackageServiceProvider.php'
<?php
namespace JohnDoe\BlogPackage;
use Illuminate\Support\ServiceProvider;
class BlogPackageServiceProvider extends ServiceProvider
{
public function register()
{
//
}
public function boot()
{
$this->app->router->group([
'namespace' => 'JohnDoe\BlogPackage\Http\Controllers',
], function ($router) {
require __DIR__.'/../routes/web.php';
});
}
}