PHP code example of chani / wajha

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

    

chani / wajha example snippets


use Safi\Wajha\WajhaCompiler;
use Safi\Wajha\WajhaDispatcher;

$compiler = new WajhaCompiler();

$compiler->get('/users', 'UserController@index');
$compiler->post('/users', 'UserController@store');

$compiledData = $compiler->compile();
$dispatcher = new WajhaDispatcher($compiledData);

// HTTP Method MUST be uppercase
$result = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);

switch ($result[0]) {
    case WajhaDispatcher::FOUND:
        $handler = $result[1];
        $vars = $result[2];
        break;

    case WajhaDispatcher::METHOD_NOT_ALLOWED:
        $allowedMethods = $result[1]; // Array of allowed HTTP methods
        break;

    case WajhaDispatcher::NOT_FOUND:
        http_response_code(404);
        break;
}

// Compiles to: /users/(\d+)
$compiler->get('/users/{id:int}', 'UserController@show');

// Compiles to RFC 4122 UUID pattern
$compiler->get('/files/{id:uuid}', 'FileController@show');

// Compiles to: /posts/([a-z0-9-]+)
$compiler->get('/posts/{slug:slug}', 'PostController@show');

enum OrderStatus: string {
    case Pending = 'pending';
    case Paid = 'paid';
    case Shipped = 'shipped';
}

// Compiled regex constraint: (pending|paid|shipped)
$compiler->get('/orders/{status:' . OrderStatus::class . '}', 'OrderController@index');

$compiler->addGroup('/api/v1', function (WajhaCompiler $api) {
    // Path: /api/v1/products
    $api->get('/products', 'ProductController@index');

    // Path: /api/v1/admin/stats
    $api->addGroup('/admin', function (WajhaCompiler $admin) {
        $admin->get('/stats', 'AdminController@stats');
    });
});

// Compiles into two discrete routes: /archive and /archive/{year:\d+}
$compiler->get('/archive[/{year:int}]', 'ArchiveController@index');

use Safi\Wajha\WajhaCompiler;
use Safi\Wajha\WajhaDispatcher;
use Safi\Wajha\WajhaUrlGenerator;

$compiler = new WajhaCompiler();

// Register named routes using named parameters
$compiler->get('/users/{id:int}', 'UserController@show', name: 'users.show');
$compiler->get('/posts/{slug:slug}', 'PostController@show', name: 'posts.show');

$compiledData = $compiler->compile();

// Inbound Dispatcher receives only matching tables
$dispatcher = new WajhaDispatcher($compiledData);

// Standalone URL Generator receives only reverse mapping table
$generator = new WajhaUrlGenerator($compiledData['reverse']);

// 1. Basic URL Interpolation
echo $generator->generate('users.show', ['id' => 42]);
// Output: /users/42

// 2. Automatic Query String Generation for excess parameters
echo $generator->generate('posts.show', [
    'slug' => 'hello-world',
    'ref' => 'newsletter',
    'page' => 2
]);
// Output: /posts/hello-world?ref=newsletter&page=2

use Safi\Wajha\WajhaUrlGenerator;
use Twig\Environment;
use Twig\TwigFunction;

/** @var Environment $twig */
/** @var WajhaUrlGenerator $generator */

$twig->addFunction(new TwigFunction('path', function (string $name, array $params = []) use ($generator): string {
    return $generator->generate($name, $params);
}));
bash
# Install development dependencies
composer tests

# Run benchmark suites
php tests/test.php
php tests/run_realworld_bench.php