1. Go to this page and download the library: Download saltid/lumen-repository library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
saltid / lumen-repository example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use SaltId\LumenRepository\Contracts\TransformableInterface;
class Article extends Model implements TransformableInterface
{
// your model content.
public function transform(): array
{
// return your model toArray() as default
return $this->toArray();
// or you can transform it into anything you wanted.
// for example :
//return [
// 'my_id' => $this->id,
// 'my_title' => $this->title,
// 'my_relation_to_other_model_id' => $this->getOtherModel()?->id,
// 'my_relation_to_other_model_to_array' => $this->getOtherModel()?->toArray(),
// 'my_relation_to_other_model_to_transform' => $this->getOtherModel()?->transform(),
//];
}
}
namespace App\Transformers;
use SaltId\LumenRepository\Transformers\TransformerAbstract;
class ArticleTransformer extends TransformerAbstract
{
namespace App\Presenters;
use App\Transformers\ArticleTransformer;
use SaltId\LumenRepository\Presenter\FractalPresenter;
use SaltId\LumenRepository\Transformers\TransformerAbstract;
class ArticlePresenter extends FractalPresenter
{
public function getTransformer(): ?TransformerAbstract
{
return app(ArticleTransformer::class);
}
namespace App\Repositories;
use SaltId\LumenRepository\Repositories\AbstractRepository;
use SaltId\LumenRepository\Contracts\PresenterInterface;
class ArticleRepository extends AbstractRepository
{
protected array $searchableFields = [
'title',
'description'
];
public function presenter(): ?PresenterInterface
{
// return should be implement PresenterInterface
return app(ArticlePresenter::class);
}
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use SaltId\LumenRepository\Contracts\RepositoryInterface;
class ArticleController extends Controller
{
protected RepositoryInterface $repository;
public function __construct(RepositoryInterface $repository)
{
$this->repository = $repository;
}
// Or you can add the line code above in \App\Http\Controllers\Controller
// so you don't need the declare it everytime. It's up to you.
public function index(): array
{
return $this->repository->all();
//return $this->repository->paginate();
}
// you may create FormRequest for validation.
public function store(Request $request): array
{
return $this->repository->create($request->all());
}
public function show(int $id): array
{
return $this->repository->find($id);
}
public function update(Request $request, int $id): array
{
return $this->repository->update($request->all(), $id);
}
public function destroy(int $id): array
{
return $this->repository->delete($id);
}
/** @var \Laravel\Lumen\Routing\Router $router */
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/
$router->get('/article', 'ArticleController@index');
$router->post('/article', 'ArticleController@store');
$router->get('/article/{id}', 'ArticleController@show');
$router->put('/article/{id}', 'ArticleController@update');
$router->delete('/article/{id}', 'ArticleController@destroy');
~~~
Since we inject the `RepositoryInterface` in our controller, we will use `service provider` to tell the controller which repository we will use on our controller.
Create your service provider class called `RepositoryServiceProvider`
~~~ php
namespace App\Providers;
use App\Http\Controllers\ArticleController;
use App\Models\Article;
use App\Repositories\ArticleRepository;
use Illuminate\Support\ServiceProvider;
use SaltId\LumenRepository\Contracts\RepositoryInterface;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app
->when(ArticleController::class)
->needs(RepositoryInterface::class)
->give(function() {
return new ArticleRepository(new Article());
});
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.