PHP code example of moeen-basra / photon

1. Go to this page and download the library: Download moeen-basra/photon 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/ */

    

moeen-basra / photon example snippets


use Illuminate\Routing\Controller as BaseController;

use MoeenBasra\Photon\Http\Controller as BaseController;

MoeenBasra\Photon\Foundation\Exceptions\Handler\Handler

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

$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,
    ]);
}

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(),
        ]);
    }
}

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));
    }
}