PHP code example of cr0w / phorq

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

    

cr0w / phorq example snippets




use phorq\Router;

$router = Router::create(
    __DIR__ . '/modules',
    __DIR__ . '/cache/routes.php'
);

$result = $router->route();

if ($result) {
    echo $result->value;
} else {
    http_response_code(404);
    echo '404 Not Found';
}

$result = $router->route($ctx);

 // routes/login.php
if ($req->isPost()) {
    // handle form submission
} else {
    // render form
}

$req    // Request object
$ctx    // whatever was passed to route() — may be null
$router // the Router instance
// + one variable per URL param, e.g. $id, $slug, $rest

$req->method          // 'GET', 'POST', …
$req->path            // 'users/42'
$req->pattern         // '/core/users/[id]'
$req->module          // 'core'
$req->string('email') // trimmed string from input or query
$req->int('page', 1)  // integer with default
$req->bool('active')  // boolean
$req->isPost()        // method checks
$req->isHtmx()        // HX-Request header present
$req->target()        // HX-Target header
$req->header('X-Foo') // arbitrary header

 // routes/api/data.php
return ['json' => ['ok' => true, 'user' => 'Alice']];

 // routes/index.php
echo '<h1>Hello</h1>';

 // modules/core/middleware.php
use phorq\{Request, Router};

return function (callable $next, Request $req, mixed $ctx, Router $router) {
    // before handler
    $result = $next();
    // after handler
    return $result;
};

return function (callable $next, Request $req) {
    if (!$req->isSecure()) {
        header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], true, 301);
        exit;
    }
    return $next();
};

$router = Router::create(
    $modulesDir,
    __DIR__ . '/cache/routes.php'
);

modules/
  core/
    middleware.php          # runs on every request
    routes/
      index.php             # GET /
      about.php             # /about
      users/
        index.php           # /users
        [id].php            # /users/:id
      docs/
        [...rest].php       # /docs/*
      pages/
        [[...path]]/
          index.php         # /pages and /pages/*
  blogging/
    config.php              # { mount: 'blog' }
    routes/
      index.php             # /blog
      [slug].php            # /blog/:slug
bash
php -S localhost:8080 example/public/index.php