PHP code example of mawsis / nebula-php

1. Go to this page and download the library: Download mawsis/nebula-php 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/ */

    

mawsis / nebula-php example snippets


// public/index.php
ateImmutable(dirname(__DIR__))->safeLoad();
Nebula\Core\Config::load(dirname(__DIR__) . '/config');

$app = new Nebula\Core\Application(dirname(__DIR__));


use Nebula\Core\Facades\Route;
use App\Controllers\AuthController;
use App\Controllers\UserController;

Route::get('/users', [UserController::class, 'listUsers'], ['auth']);
Route::get('/users/{id}', [UserController::class, 'showUser'], ['auth']);
Route::post('/login', [AuthController::class, 'loginStore']);

public function listUsers(Request $request, Response $response)
{
    $users = Paginator::paginate(User::query()->orderBy('id', 'ASC'), $response);

    return $response->json(['users' => $users]);
}

namespace App\Models;

use Nebula\Core\DbModel;

class Post extends DbModel
{
    public static function tableName(): string  { return 'posts'; }
    public static function primaryKey(): string { return 'id'; }
    public static function attributes(): array  { return ['title', 'body', 'user_id']; }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}

$post = Post::findOne(['id' => 42]);

$posts = Post::query()
    ->where('user_id', '=', $userId)
    ->orderBy('created_at', 'DESC')
    ->limit(10)
    ->get();

$posts = Post::with(['user'])->where('title', 'LIKE', '%nebula%')->get(); // eager loading

Post::create(['title' => 'Hello', 'body' => '...', 'user_id' => 1]);

namespace App\Requests;

use Nebula\Core\Request;

class RegisterRequest extends Request
{
    public function rules(): array
    {
        return [
            'username' => ['

public function registerStore(RegisterRequest $request, Response $response)
{
    $data = $request->validated();

    User::create([
        'username' => $data['username'],
        'email'    => $data['email'],
        'password' => password_hash($data['password'], PASSWORD_DEFAULT),
    ]);

    return $response->success(['registered' => true], 201);
}

return [
    'auth' => Nebula\Core\Middlewares\AuthMiddleware::class,
    'jwt'  => Nebula\Core\Middlewares\JwtMiddleware::class,
    'csrf' => Nebula\Core\Middlewares\CsrfMiddleware::class,
    'cors' => Nebula\Core\Middlewares\CorsMiddleware::class,
    'json' => Nebula\Core\Middlewares\JsonMiddleware::class,
];

use Nebula\Core\Facades\Auth;
use Nebula\Core\Middlewares\BaseMiddleware;
use Nebula\Core\Exceptions\ForbiddenException;

class AdminMiddleware extends BaseMiddleware
{
    public function execute()
    {
        if (Auth::isGuest() || !Auth::user()->is_admin) {
            throw new ForbiddenException();
        }
    }
}
bash
composer