1. Go to this page and download the library: Download yesccx/better-laravel 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/ */
php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Yesccx\BetterLaravel\Exceptions\ExceptionHandlerManager;
class Handler extends ExceptionHandler
{
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
ExceptionHandlerManager::register($this);
// Do something....
}
}
php
// routes/modules/user.php
use Illuminate\Support\Facades\Route;
Route::group([
'prefix' => 'user'
], function() {
// Create user
Route::get('create', [\App\Http\Controller\UserController::class, 'create']);
// Get userinfo
Route::get('info', [\App\Http\Controller\UserController::class, 'info']);
});
php
// routes/modules/content.php
use Illuminate\Support\Facades\Route;
Route::group([
'prefix' => 'content'
], function() {
// Get list
Route::get('list', [\App\Http\Controller\ContentController::class, 'list']);
// Get info
Route::get('info', [\App\Http\Controller\ContentController::class, 'info']);
});
php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Yesccx\BetterLaravel\Exceptions\ExceptionHandlerManager;
class Handler extends ExceptionHandler
{
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
ExceptionHandlerManager::register($this);
}
}
php
class UserService {
use InstanceMake;
public function __construct(
public int $id = 0
) {}
};
UserService::make(['id' => 1]);
php
class UserService {
use InstanceMake;
public function __construct(
public UserModel $model
) {}
};
$user = UserService::resolve();
$user->model->find(['id' => 1]);
php
class UserService {
use InstanceMake;
public function __construct(
public UserModel $model
) {}
};
$user = UserService::resolve();
$user->model->find(['id' => 1]);
// 再次调用时不会重复实例化类
UserService::resolve();
// 指定强制重新实例化类
UserService::resolve(force: true);
php
use Yesccx\BetterLaravel\Traits\InitializeTraits;
trait RecordLog {
protected \Illuminate\Log\Logger $logger;
public function initializeRecordLog()
{
$this->logger = app(\Illuminate\Log\Logger::class);
}
public function record(string $message)
{
$this->logger->info($message);
}
}
class User {
use InitializeTraits, RecordLog;
}
$user = new User;
$user->record('test');
php
class User {
use InitializeTraits {
__construct as __traitConstruct;
}, RecordLog;
public function __construct()
{
$this->__traitConstruct();
}
}
php
final class AccountLoginRequest extends BaseRequest
{
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes()
{
return [
'username' => '用户名',
'password' => '密码',
];
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'username' => ['bail', '
php
use Yesccx\BetterLaravel\Validation\BaseRequest;
use Yesccx\BetterLaravel\Validation\Traits\ValidatorScenes;
final class UserRequest extends BaseRequest
{
use ValidatorScenes;
/**
* 验证场景
*
* @return array
*/
public function scenes(): array
{
return [
'create' => [
'username', 'password', 'nickname', 'introduction'
],
'update' => [
'nickname', 'introduction'
]
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes()
{
return [
'username' => '用户名',
'password' => '密码',
'nickname' => '昵称',
'introduction' => '简介'
];
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'username' => ['bail', '
php
class UserController
{
public function create(UserRequest $request)
{
// 此处会验证 'username', 'password', 'nickname', 'introduction' 字段
// Do something ...
}
public function update(UserRequest $request)
{
// 此处仅验证 'nickname', 'introduction' 字段
// Do something ...
}
}