PHP code example of l0n3ly / laravel-repository-with-service
1. Go to this page and download the library: Download l0n3ly/laravel-repository-with-service 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/ */
l0n3ly / laravel-repository-with-service example snippets
class UserController extends Controller
{
public function __construct(
protected UserRepository $repository,
protected UserService $service
) {}
public function index()
{
return $this->service->getActiveUsers();
}
}
return [
'repository_directory' => 'app/Repositories',
'repository_namespace' => 'App\Repositories',
'service_directory' => 'app/Services',
'service_namespace' => 'App\Services',
// ... naming conventions
];
$this->app->extend(UserRepository::class, function ($service, $app) {
return new CachedUserRepository($service);
});
class UserServiceImplement implements UserService
{
use ResultService;
public function create(array $data): array
{
try {
$user = $this->repository->create($data);
return $this->success($user, 'User created');
} catch (Exception $e) {
return $this->error($e->getMessage());
}
}
}
public function test_service_logic()
{
$repository = Mockery::mock(UserRepository::class);
$repository->shouldReceive('find')->with(1)->andReturn(['id' => 1]);
$this->app->bind(UserRepository::class, fn() => $repository);
$service = app(UserService::class);
$result = $service->getUser(1);
$this->assertEquals(1, $result['id']);
}
bash
php artisan boost:install --discover
bash
php artisan make:repository Post --service --api
php artisan make:repository Category --service
php artisan make:repository Post/Comment --service
bash
php artisan make:repository Product --service --api
php artisan make:repository Order --service --api
php artisan make:service Shop/Checkout --api
php artisan make:service Shop/Inventory --api
bash
php artisan make:repository Tenant --service --api
php artisan make:repository User --service --api
php artisan make:repository Subscription --service --api