PHP code example of grogorick / php-routing

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

    

grogorick / php-routing example snippets


# api.php

ick\PhpRouting as R;

...

function get_feed($GET_data) {
  $feed_data = ...
  if ($feed_data)
    R\respond($feed_data);
  else
    R\respond(null, R\Response::ERROR_NOT_FOUND);
}

...

R\route([
  'v1' => [
    'sign-in' => [
      'POST' => 'App\Auth\sign_in'
    ],
    '(authenticated)' => R\Check('App\Auth\verify_authorization_header', [
      'accounts' => R\Entity([
        'POST' => fn() => R\respond('create account'),
        'GET' => fn() => R\respond('list accounts'),
        '/\w+/' => fn($account_slug) => R\Item([
          'GET' => fn() => R\respond('get account ' . $account_slug),
          'PUT' => fn() => R\respond('replace account ' . $account_slug),
          'PATCH' => fn() => R\respond('update account ' . $account_slug),
          'DELETE' => fn() => R\respond('delete account ' . $account_slug)
        ]),
        ...
      ]),
      'posts' => R\Entity([
        /* post_id */ '/\d+/' => R\IntParam([
          'GET' => fn($post_id) => R\respond('get post ' . $post_id),
          ...
        ]),
        ...
      ]),
      'feed' => [
        'GET' => fn($GET_data) => get_feed($GET_data),
        ...
      ],
      ...
    ]),
    ...
  ],
  ...
]);

R\set_response_headers([
  "Access-Control-Allow-Origin: $approved_request_origin",
  'Access-Control-Allow-Headers: content-type',
  'Content-Type: application/json; charset=UTF-8'
]);
bash
composer