Package Data | |
---|---|
Maintainer Username: | YaangVu |
Maintainer Contact: | yaangvu@gmail.com (Yaang Vu) |
Package Create Date: | 2021-04-18 |
Package Last Update: | 2024-01-12 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-18 03:01:10 |
Package Statistics | |
---|---|
Total Downloads: | 8,846 |
Monthly Downloads: | 192 |
Daily Downloads: | 0 |
Total Stars: | 7 |
Total Watchers: | 2 |
Total Forks: | 1 |
Total Open Issues: | 0 |
This base will help to create simple API (CRUD) for 1 specific entity, such as User
composer require yaangvu/laravel-base
.
├── src
│ ├── Constants
│ │ ├── DataCastConstant.php
│ │ └── OperatorConstant.php
│ ├── Controllers
│ │ └── BaseController.php
│ ├── Exceptions
│ │ ├── BadRequestException.php
│ │ ├── BaseException.php
│ │ ├── ForbiddenException.php
│ │ ├── GatewayTimeOutException.php
│ │ ├── Handler.php
│ │ ├── NotFoundException.php
│ │ ├── SystemException.php
│ │ └── UnauthorizedException.php
│ ├── Helpers
│ │ ├── FileHelper.php
│ │ ├── LocalFileHelper.php
│ │ ├── QueryHelper.php
│ │ └── RouterHelper.php
│ ├── LaravelBaseServiceProvider.php
│ ├── Services
│ │ ├── BaseServiceInterface.php
│ │ └── impl
│ └── config
│ └── laravel-base.php
use YaangVu\LaravelBase\Helpers\RouterHelper;
RouterHelper::resource($router, '/users', 'UserController');
use App\Models\User;
use YaangVu\LaravelBase\Services\impl\BaseService;
class UserService extends BaseService
{
function createModel(): void
{
$this->model = new User();
}
}
use App\Services\UserService;
use YaangVu\LaravelBase\Controllers\BaseController;
class UserController extends BaseController
{
public function __construct()
{
$this->service = new UserService();
parent::__construct();
}
}
To insert or update an entity, you must define columns can give data
class User extends Model
{
protected $fillable
= [
'username', 'email', 'password', 'first_name', 'last_name'
];
}
$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
];
{param-name}{operator} = {value}
username = admin
----> username
equal admin
name__~ = super
----> name
like %super%
age__gt = 18
----> age
gather than 18
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'
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);
}
}
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);
}
}
It supports these observe function:
preAdd
postAdd
preUpdate
postUpdate
preDelete
postDelete
preGet
postGet
preGetAll
postGetAll
use YaangVu\LaravelBase\Helpers\LocalFileHelper;
$fileHelper = new LocalFileHelper();
if ($request->video)
$videoPath = $fileHelper->upload($request, 'video', 'university/video');