PHP code example of vireo / framework

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

    

vireo / framework example snippets


// Using helper functions
route('users.show', ['id' => 123]);  // Generate URL
redirect('dashboard');                // Redirect to named route



namespace App\Features\Users;

use Vireo\Framework\Http\Controller;

class ShowUser extends Controller
{
    public function __invoke(int $id)
    {
        $user = table('users')->where('id', $id)->first();

        // Return JSON for API requests
        if ($this->isApi()) {
            return $this->json(['user' => $user]);
        }

        // Return Inertia response for web
        return $this->inertia('Users/Show', ['user' => $user]);
    }
}

// Simple queries
$users = table('users')
    ->where('active', true)
    ->orderBy('created_at', 'desc')
    ->get();

// Joins and relationships
$posts = table('posts')
    ->join('users', 'posts.user_id', '=', 'users.id')
    ->select('posts.*', 'users.name as author')
    ->get();

// Spatial queries (PostGIS)
$nearby = spatial('locations')
    ->withinDistance('geom', $point, 1000)
    ->get();

$errors = validate($_POST, [
    'email' => ['irmed'],
    'age' => ['numeric', 'min:18'],
]);

if (!empty($errors)) {
    return $this->validationError($errors);
}

// Render an Inertia component
inertia('Dashboard/Index', [
    'stats' => $stats,
    'recentActivity' => inertia_lazy(fn() => $this->getRecentActivity()),
]);

// Flash messages
inertia_flash('success', 'Profile updated successfully!');

// Validation errors
inertia_errors(['email' => 'This email is already taken']);

// Check permissions
if (can('users.edit')) {
    // User can edit users
}

// Check with attribute
if (can('projects.edit', null, 'department', 'engineering')) {
    // User can edit projects in engineering department
}

// Multiple permission check
if (can_any(['users.view', 'users.edit'])) {
    // User has at least one permission
}



use Vireo\Framework\Database\Migrations\Migration;

return new class extends Migration
{
    public function up(): void
    {
        $this->create('users', function ($table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        $this->drop('users');
    }
};

// Get config value
$appName = config('app.name');
$dbHost = config('database.connections.mysql.host');

// Get with default
$debug = config('app.debug', false);

// Get environment variable
$apiKey = env('API_KEY');

// With default value
$debug = env('APP_DEBUG', false);

// Set session value
session_set('user_id', 123);

// Get session value
$userId = session_get('user_id');

// Check if exists
if (session_has('user_id')) {
    // ...
}

// Remove session value
session_forget('user_id');

// Log messages at different levels
log_debug('Debug information', ['context' => $data]);
log_info('User logged in', ['user_id' => $userId]);
log_warning('Deprecated feature used');
log_error('Something went wrong', ['exception' => $e->getMessage()]);

// Store a file
storage()->put('uploads/avatar.jpg', $fileContents);

// Get file contents
$contents = storage()->get('uploads/avatar.jpg');

// Check if file exists
if (storage()->exists('uploads/avatar.jpg')) {
    // ...
}

// Delete a file
storage()->delete('uploads/avatar.jpg');

// config/Permissions.php
return [
    'roles' => [
        'admin' => ['manager', 'user'],
        'manager' => ['user'],
        'user' => [],
    ],
    'permissions' => [
        'users.view' => '*',                    // Public access
        'users.create' => ['admin', 'manager'],
        'users.edit' => ['admin', 'manager'],
        'users.delete' => ['admin'],
    ],
    'super_admin' => [
        'roles' => ['superadmin'],
        'bypass_all' => true,
    ],
];