PHP code example of vexor / framework

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

    

vexor / framework example snippets


// Temel route'lar
$router->get('/users', 'UserController@index');
$router->post('/users', 'UserController@store');
$router->put('/users/{id}', 'UserController@update');
$router->delete('/users/{id}', 'UserController@destroy');

// Closure route
$router->get('/ping', function(Request $request): Response {
    return Response::json(['pong' => true]);
});

// Named route
$router->get('/profile', 'ProfileController@show')->name('profile');

// Route grubu
$router->prefix('/admin')->middleware('auth')->group(function($r) {
    $r->get('/dashboard', 'AdminController@dashboard');
});

// RESTful resource (tam CRUD)
$router->resource('posts', PostController::class);

// API resource (sadece index, show, store, update, destroy)
$router->apiResource('posts', PostController::class);

class UserController extends Controller
{
    public function index(Request $request): Response
    {
        // Input alma (XSS korumalı)
        $search = $request->safe('search');
        
        return $this->json(['users' => []]);
    }

    public function store(Request $request): Response
    {
        // Validation
        $data = $this->validate($request, [
            'name'     => '

// Fluent QueryBuilder — hazırlanmış ifadeler, SQL injection imkansız
$users = User::query()
    ->where('is_active', true)
    ->whereLike('name', '%john%')
    ->whereIn('role', ['admin', 'editor'])
    ->orderBy('created_at', 'DESC')
    ->limit(10)
    ->get();

// ActiveRecord
$user = User::find(1);
$user->name = 'Jane';
$user->save();

// İlişkiler
class Post extends Model {
    public function author() { return $this->belongsTo(User::class, 'user_id'); }
    public function comments() { return $this->hasMany(Comment::class, 'post_id'); }
}

// Toplu işlemler
User::where('is_active', false)->delete();

// Transaction
DB::transaction(function ($db) {
    $db->table('accounts')->where('id', 1)->decrement('balance', 100);
    $db->table('accounts')->where('id', 2)->increment('balance', 100);
});

// Sayfalama
$users = User::query()->paginate(15, $page);
// ['data' => [...], 'total' => 100, 'per_page' => 15, 'last_page' => 7]

// View'da
<?= csrf_field() 

// Output encoding
e($user->name)                   // HTML context
security()->escape($val, 'js')   // JS context
security()->purify($html)        // HTML sanitize

// Route bazlı
$router->middleware('RateLimitMiddleware')->group(...);

// Manuel
if (!security()->rateLimit('login:' . $ip, 5, 60)) {
    abort(429, 'Too many attempts.');
}

$encrypted = security()->encrypt('gizli veri');
$decrypted = security()->decrypt($encrypted);

$hash = security()->hashPassword('password123');
$ok   = security()->verifyPassword('password123', $hash);

// Giriş
$success = auth()->attempt(['email' => '[email protected]', 'password' => '...']);

// Oturumu kapat
auth()->logout();

// Mevcut kullanıcı
$user = auth()->user();
if (auth()->check()) { ... }

// Token oluştur
$token = auth()->generateJwt($user, ttl: 3600);

// İstemci: Authorization: Bearer {token}
// Vexor otomatik doğrular

// Key oluştur
$key = security()->generateApiKey('myapp');

// İstemci: X-API-Key: {key} veya ?api_key={key}
// Vexor otomatik doğrular

// Secret oluştur
$secret = security()->generate2FASecret();

// Kod doğrula (Google Authenticator vb.)
$valid = security()->verifyTotp($secret, $inputCode);

class SendEmailsCommand extends Command
{
    public function getName(): string        { return 'emails:send'; }
    public function getDescription(): string { return 'Send pending emails'; }

    public function execute(Input $input, Console $console): int
    {
        $limit = $input->option('limit', 100);
        $this->info("Sending up to {$limit} emails...");

        // İş mantığı burada
        $this->success('Done!');
        return 0;
    }
}

class RoleMiddleware implements MiddlewareInterface
{
    public function handle(Request $request, Closure $next): Response
    {
        $user = $request->getAttribute('user');

        if (!$user?->isAdmin()) {
            return Response::json(['error' => 'Forbidden'], 403);
        }

        return $next($request);
    }
}