定制 moeen/photon 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

moeen/photon

最新稳定版本:1.0.0

Composer 安装命令:

composer require moeen/photon

包简介

Laravel micro framework for SRP (Single Responsibility Principle)

README 文档

README

License

Photon is a lightweight extension framework for Laravel that implements the Single Responsibility Principle (SRP) design pattern. It draws inspiration from the Lucid Framework.

How to Use

To use Photon in your Laravel project, follow these steps:

  1. Open your terminal and execute the following command:
composer require moeen-basra/photon
  1. Open the App\Http\Controllers\Controller class and replace the following code:
use Illuminate\Routing\Controller as BaseController;

with:

use MoeenBasra\Photon\Http\Controller as BaseController;
  1. Extend the app\Exceptions\Handler class with the following class:
MoeenBasra\Photon\Foundation\Exceptions\Handler\Handler

Alternatively, you can use the following traits in your existing exception handler:

use MoeenBasra\Photon\Concerns\Marshal;
use MoeenBasra\Photon\Concerns\ActionRunner;

In the render method, execute the following job if the request accepts application\json:

$message = $exception->getMessage();
$code = $exception->getCode();
$errors = ($exception instanceof \Illuminate\Validation\ValidationException) ? $exception->errors() : [];

if ($request->expectsJson()) {
    return $this->run(JsonErrorResponseJob::class, [
        'message' => $message,
        'errors' => $errors,
        'status' => ($code < 100 || $code >= 600) ? 400 : $code,
    ]);
}
  1. Create the following directories inside the app folder:
|-- app
|  |-- Actions
|  |-- Features
|  |-- Operations
  1. Here's an example code for a controller that serves the feature:
namespace App\Http\Controllers\Api\Auth;

use App\Features\Api\Auth\RegisterFeature;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use MoeenBasra\Photon\Http\Controller;

class AuthController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:api', ['only' => ['me', 'logout']]);
    }

    public function register(Request $request): JsonResponse
    {
        return $this->serve(RegisterFeature::class, [
            'data' => $request->validated(),
        ]);
    }
}
  1. Lastly, here's an example code for a feature that runs the job:
namespace App\Features\Api\Auth;

use App\Operations\Auth\RegisterOperation;
use Illuminate\Http\JsonResponse;
use Photon\Actions\JsonResponseAction;
use MoeenBasra\Photon\Features\Feature;

final class RegisterFeature extends Feature
{
    public function __construct(
        readonly private array $input
    ){}
    
    public function handle(): JsonResponse
    {
        $data = $this->run(RegisterOperation::class, [
            'input' => $this->input
        ]);

        return $this->run(new JsonResponseAction($data));
    }
}

License

Photon is released under the MIT License. See the LICENSE file for details.

Contact

You can reach me here moeen.basra@gamil.com

统计信息

  • 总下载量: 1.86k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-11-14