cloud_download Stáhnout

Routování

.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?_route=$1 [L,QSA]

index.php

Hlavní soubor

  • Vytvoří se "request"
  • Předá se jádru frameworku
  • Vrátí "response" a vyrenderuje ho.

To je celé kouzlo.

<?php

use Gephart\Framework\Kernel;
use Gephart\Http\RequestFactory;

include_once __DIR__ . "/../vendor/autoload.php";

$request = (new RequestFactory())->createFromGlobals();

$kernel = new Kernel($request);

$response = $kernel->run();
echo $kernel->render($response);

/config/routing.json

Autoloading routes from controllers

{
  "autoload": "src/"
}

/src/App/Controller/DefaultController.php

<?php

namespace App\Controller;

use Gephart\Framework\Facade\Response;

class DefaultController
{
    /**
     * @Route /
     */
    public function index() {
        return Response::text("Hello World");
    }
}

Nastavení routy:

/**
 * @Route {
 *  "rule": "/page/{slug}/{limit}/{offset}",
 *  "name": "page_detail",
 *  "requirements": {
 *      "limit": "[0-9]+",
 *      "offset": "[0-9]+"
 *  }
 * }
 */
public function index($slug, $limit, $offset) {
    return Response::text("Hello " . $slug);
}

RoutePrefix

Následující nastavení odpovídá požadavku "/admin/page".

<?php

namespace App\Controller;

use Gephart\Framework\Facade\Response;

/**
 * @RoutePrefix /admin
 */
class AdminController
{
    /**
     * @Route /page
     */
    public function index() {
        return Response::text("Hello Admin");
    }
}

Generování URL

<?php

namespace App\Controller;

use Gephart\Framework\Facade\Response;
use Gephart\Framework\Facade\Router;

class DefaultController
{
    /**
     * @Route {
     *  "rule": "/page/{slug}/{limit}/{offset}",
     *  "name": "page_detail",
     *  "requirements": {
     *      "limit": "[0-9]+",
     *      "offset": "[0-9]+"
     *  }
     * }
     */
    public function index($slug, $limit, $offset) {
        $url = Router::generateUrl("page_detail", [
            "slug" => "articles",
            "limit" => "10",
            "offset" => "20",
        ]);

        return Response::text("Hello World - " . $url);
    }
}