yaangvu / laravel-base
Laravel API Resource Base
Installs: 8 889
Dependents: 4
Suggesters: 0
Security: 0
Stars: 7
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ^8.1|^8.2
- ramsey/uuid: ^4.7.1
Requires (Dev)
- illuminate/console: ^7|^8|^9|^10
- illuminate/database: ^7|^8|^9|^10
- illuminate/http: ^7|^8|^9|^10
- illuminate/pagination: ^7|^8|^9|^10
- dev-master
- v4.x-dev
- v4.0.42
- v4.0.41
- v4.0.40
- v4.0.39
- v4.0.38
- v4.0.37
- v4.0.36
- v4.0.35
- v4.0.34
- v4.0.33
- v4.0.32
- v4.0.31
- v4.0.30
- v4.0.29
- v4.0.28
- v4.0.27
- v4.0.26
- v4.0.25
- v4.0.24
- v4.0.23
- v4.0.22
- v4.0.21
- v4.0.20
- v4.0.19
- v4.0.18
- v4.0.17
- v4.0.16
- v4.0.15
- v4.0.14
- v4.0.13
- v4.0.12
- v4.0.11
- v4.0.10
- v4.0.9
- v4.0.8
- v4.0.7
- v4.0.6
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v3.x-dev
- v3.0.12
- v3.0.11
- v3.0.10
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.x-dev
- v2.2.1
- v2.2.0
- v2.1.12
- v2.1.11
- v2.1.10
- v2.1.9
- v2.1.8
- v2.1.7
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.1.x-dev
- v1.1.46
- v1.1.45
- v1.1.44
- v1.1.43
- v1.1.42
- v1.1.41
- v1.1.40
- v1.1.39
- v1.1.38
- v1.1.37
- v1.1.36
- v1.1.35
- v1.1.34
- v1.1.33
- v1.1.32
- v1.1.31
- v1.1.30
- v1.1.29
- v1.1.28
- v1.1.27
- v1.1.26
- v1.1.25
- v1.1.24
- v1.1.23
- v1.1.22
- v1.1.21
- v1.1.20
- v1.1.19
- v1.1.18
- v1.1.17
- v1.1.16
- v1.1.15
- v1.1.14
- v1.1.13
- v1.1.12
- v1.1.11
- v1.1.10
- v1.1.9
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.41
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-hoangky
- dev-hungnv1
- dev-namhp
- dev-duyanh
- dev-duynam
- dev-viethqb
- dev-multiple_delete
- dev-tungnd_add_getbycode
- dev-dev-master
This package is auto-updated.
Last update: 2024-11-12 09:31:35 UTC
README
This base will help to create simple API (CRUD) for 1 specific entity
Install
composer require yaangvu/laravel-base
For Laravel
Publish configuration file and Base Classes
php artisan vendor:publish --provider="YaangVu\LaravelBase\Providers\BaseServiceProvider"
For lumen
cp vendor/yaangvu/laravel-base/src/config/laravel-base.php config/laravel-base.php mkdir -p app/Base cp vendor/yaangvu/laravel-base/src/Base/Publish/Controller.php app/Base/Controller.php cp vendor/yaangvu/laravel-base/src/Base/Publish/Service.php app/Base/Service.php
Generator Command
If you want to use Generator Command, Add the following class to the providers
array in config/app.php
:
YaangVu\LaravelBase\Provider\GeneratorServiceProvider::class,
If you want to manually load it only in non-production environments, instead you can add this to
your AppServiceProvider
with the register()
method:
public function register() { if ($this->app->isLocal()) { $this->app->register(\YaangVu\LaravelBase\Provider\GeneratorServiceProvider::class); } // ... }
Initial API resource
Generate code
php artisan yaangvu:base Post <option>
Option:
- -S: generate code with default Swagger annotation
- -i: Auto inject Service in Controller methods
Directory structure of generated code
├── app
│ ├── Domains
│ │ └── Post
│ │ ├── Controllers
│ │ │ └── PostController.php
│ │ ├── Models
│ │ │ └── Post.php
│ │ └── Services
│ │ └── PostService.php
Route
Route::base('/posts', \App\Domains\Post\Controllers\PostController::class);
Usage
Dynamic query parameters
Operators supported
$operators
= [
'__gt' => OperatorConstant::GT, // Greater than
'__ge' => OperatorConstant::GE, // Greater than or equal
'__lt' => OperatorConstant::LT, // Less than
'__le' => OperatorConstant::LE, // Less than or equal
'__~' => OperatorConstant::LIKE // Like
];
To query, you can add more params with format:
{param-name}{operator} = {value}
Example:
username = admin
---->username
equaladmin
name__~ = super
---->name
like%super%
age__gt = 18
---->age
gather than18
Full request example
Request to query user with username=admin
and name LIKE %super%
and age > 18
curl --location --request GET 'http://localhost:8000/api/v1/users?username=admin&name__~=super&age__gt=18'
Validate before Add an entity
Support full Laravel validation: Validation
class UserService extends BaseService
{
public function storeRequestValidate(object $request, array $rules = []): bool|array
{
$rules = [
'username' => 'required|max:255|unique:users',
];
return parent::storeRequestValidate($request, $rules);
}
}
Validate before Update an entity
Support full Laravel validation: Validation
class UserService extends BaseService
{
public function updateRequestValidate(int|string $id, object $request, array $rules = []): bool|array
{
$rules = [
'username' => 'required|max:255|unique:users,id',
];
return parent::updateRequestValidate($id, $request, $rules);
}
}
Service Observe
It supports these observe function:
function postAdd()
function postUpdate()
function postDelete()
function postGet()
function postGetAll()
Cache data
If you want to cache data when create
update
select
, implement ShouldCache
interface
class UserService extends BaseService implements \YaangVu\LaravelBase\Base\Contract\ShouldCache {}