PHP code example of pinkcrab / perique-route

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

    

pinkcrab / perique-route example snippets


// @file: plugin.php

$app_factory->module( \PinkCrab\Route\Module\Route::class );


class Some_Route extends Route_Controller {

    // @al, access to constructor, so allows for full DI.
    protected $some_service;
    protected function __construct(Service $service){
        $this->some_service = $service;
    }

    // @    $factory->post('/users', [$this->some_service, 'new_user' ]),
            
            // Create your groups using the group builder.
            $factory->group_builder('/users/{id}', function( Route_Group $group) : Route_Group {
                // Define the GET method.
                $group->get([$this->some_service, 'show_user'])
                    ->argument( // Define the argument proprties as per WP API
                        Integer_Type::on('id')
                            ->validate('is_numeric')
                            ->sanitization('absint')
                            ->

//file: config/registration.php
return [
    ...other classes
    Some_Route::class,
];
 
$route = new Route('POST', 'route/path');
$route->namespace('the-thing/v1');
$route->callback(function( WP_REST_Request $request ) {
    // Do the things
});

$route->authentication(function(WP_REST_Request $request): bool{
    return something_check($request);
});

$group = new Route_Group('my_endpoints/v2','route/');
$group->authentication('shared_group_auth')
$group->get('some_callable')
    ->authentication('additional_auth_for_get_only');
$group->post('some_other_callable');

$factory = new Route_Factory('my_endpoints/v2');
$get = $factory->get('/endpoint', 'some_callable');
$post = $factory->get('/endpoint_2', 'some_other_callable');

$route = $factory->get('the/route/{name}', [$this, 'some_callback_for_get']);
$route = $factory->post('the/route/{name}', [$this, 'some_callback_for_post']);
$route = $factory->put('the/route/{name}', [$this, 'some_callback_for_put']);
$route = $factory->patch('the/route/{name}', [$this, 'some_callback_for_patch']);
$route = $factory->delete('the/route/{name}', [$this, 'some_callback_for_delete']);

$group = $factory->group_builder('the/route/{name}', function(Route_Group $group){
    $group->get([$this, 'some_callback_for_get']);
    $group->post([$this, 'some_callback_for_post']);
    $group->delete([$this, 'some_callback_for_delete']);
    $group->put([$this, 'some_callback_for_put']);
    $group->path([$this, 'some_callback_for_path']);
});

$manager = new Route_Manager(
    new WP_Rest_Registrar(),
    new Hook_Loader()
);

// Add routes and groups
$manager->from_route(new Route(...));
$manager->from_group(new Route_Group(...));

// Dispatch
$manager->execute();