PHP code example of omega-mvc / omega

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

    

omega-mvc / omega example snippets


// database/migrations/<timestamp>_profiles.php
use Omega\Database\Facades\Schema;
use Omega\Database\Schema\Table\Create;

return [
    'up' => [
        Schema::table('profiles', function (Create $column) {
            $column('user')->varChar(32);
            $column('real_name')->varChar(100);
            $column->primaryKey('user');
        }),
    ],
    'down' => [
        Schema::drop()->table('profiles'),
    ],
];

// app/Http/Controllers/ProfileController.php
namespace App\Http\Controllers;

use App\Models\Profile;
use Omega\Database\ConnectionInterface;
use Omega\Http\Response;

use function Omega\View\view;

class ProfileController extends AbstractController
{
    public function handle(ConnectionInterface $pdo): Response
    {
        return view('profile', [
            'name' => Profile::find('omega-mvc', $pdo)->real_name
        ]);
    }
}

// resources/views/profile.template.php
{% extend('base/base.template.php') %}
{% section('title', 'Welcome {{ $name }}') %}

{% section('content') %}
    <h1>Hello, {{ $name }}! 👋</h1>
{% endsection %}

// routes/web.php
Router::get('/profile', [ProfileController::class, 'handle']);

// app/Services/ProfileServices.php
use App\Models\Profile;
use Omega\Cache\Facade\Cache;
use Omega\Database\ConnectionInterface;
use Omega\Http\JsonResponse;
use Omega\Router\Attribute\Middleware;
use Omega\Router\Attribute\Name;
use Omega\Router\Attribute\Route\Get;

class ProfileServices
{
    #[Get('/api/v1/profile')]
    #[Name('api.v1.profile')]
    #[Middleware([AuthMiddleware::class])]
    public function index(ConnectionInterface $pdo): JsonResponse
    {
        $data = Cache::remember('profile', fn () => [
            'name'   => Profile::find('omega-mvc', $pdo)->real_name,
            'status' => 200,
        ], 3600);

        return new JsonResponse($data);
    }
}

Router::register([
    ProfileServices::class,
    // add more class
]);

Router::get('/api/v1/profile', [ProfileServices::class, 'index'])
    ->name('api.v1.profile')
    ->middleware([AuthMiddleware::class]);
bash
php omega serve
bash
php omega make:migration profiles
php omega db:create  # Only if database doesn't exist yet
bash
php omega migrate
bash
php omega make:controller Profile
bash
php omega make:view profile