Verze: 0.3
- 0.6 (master)
- 0.5
- 0.4
- 0.3
- 0.2
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 DI container
- Nastaví se hlavní složka konfigurace
- Spustí router
To je celé kouzlo.
<?php
use Gephart\DependencyInjection\Container;
use Gephart\Configuration\Configuration;
use Gephart\Routing\Router;
include_once __DIR__ . "/../vendor/autoload.php";
$container = new Container();
$configuration = $container->get(Configuration::class);
$configuration->setDirectory(__DIR__ . "/config");
$router = $container->get(Router::class);
$router->run();
/config/routing.json
Autoloading routes from controllers
{
"autoload": "src/"
}
/src/App/Controller/DefaultController.php
<?php
namespace App\Controller;
use Gephart\Response\Response;
class DefaultController
{
/**
* @Route /
*/
public function index() {
return new Response("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 new Response("Hello " . $slug);
}
RoutePrefix
Následující nastavení odpovídá požadavku "/admin/page".
<?php
namespace App\Controller;
use Gephart\Response\Response;
/**
* @RoutePrefix /admin
*/
class AdminController
{
/**
* @Route /page
*/
public function index() {
return new Response("Hello Admin");
}
}
Generování URL
<?php
namespace App\Controller;
use Gephart\Response\Response;
use Gephart\Routing\Router;
class DefaultController
{
/**
* @var Router
*/
private $router;
public function __construct(Router $router)
{
$this->router = $router;
}
/**
* @Route {
* "rule": "/page/{slug}/{limit}/{offset}",
* "name": "page_detail",
* "requirements": {
* "limit": "[0-9]+",
* "offset": "[0-9]+"
* }
* }
*/
public function index($slug, $limit, $offset) {
$url = $this->router->generateUrl("page_detail", [
"slug" => "articles",
"limit" => "10",
"offset" => "20",
]);
return new Response("Hello World - " . $url);
}
}