PHP code example of ngyuki / route-collector

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

    

ngyuki / route-collector example snippets


$registry = new RouteRegistry();
$r = new RouteCollector($registry);

// GET / -> HomeController::index
$r->path('/')->get()->controller('HomeController')->action('index');

// GET|POST /both -> HomeController::both
$r->path('/both')->get()->post()->controller('HomeController')->action('both');

// GET|POST /both -> HomeController::method
$r->path('/method')->method('GET|POST')->controller('HomeController')->action('method');

$r->controller('UserController')->group(function (RouteCollector $r) {
    $r->path('/user')->group(function (RouteCollector $r) {

        // GET /user -> UserController::index
        $r->get()->action('index');

        // GET /user/create -> UserController::create
        $r->path('/create')->get()->action('create');

        // POST /user/create -> UserController::store
        $r->path('/create')->post()->action('store');
    });
    $r->path('/user/{id}')->group(function (RouteCollector $r) {

        // GET /user/{id} -> UserController::show
        $r->get()->action('show');

        // GET /user/{id}/edit -> UserController::edit
        $r->path('/edit')->get()->action('edit');

        // PUT /user/{id}/edit -> UserController::update
        $r->path('/edit')->put()->action('update');

        // DELETE /user/{id}/edit -> UserController::delete
        $r->path('/edit')->delete()->action('delete');
    });
});

return $registry->getRoutes();

$r->controller('HomeController')->action('index');
$r->param('controller', 'HomeController')->param('action', 'index');

// これで1つのルート定義
$r->path('/')->get()->controller('HomeController')->action('index');

// ↑と同じルート定義
$r->action('index')->controller('HomeController')->get()->path('/');

// GET と POST の両方にマッチします
$r->path('/')->get()->post()->controller('HomeController')->action('index');

// GET と POST の両方にマッチします
$r->path('/')->method('GET|POST')->controller('HomeController')->action('index');

// '/aaa/bbb' にマッチします
$r->path('/aaa')->path('/bbb')->get()->controller('HomeController')->action('index');

// params(['aaa' => 9, 'bbb' => 2, 'ccc' => 3]) とマージされる
$r->path('/')->get()
    ->params(['aaa' => 1])
    ->params(['aaa' => 9, 'bbb' => 2])
    ->params(['ccc' => 3]);

$r->controller('UserController')->group(function (RouteCollector $r) {
    // このブロックのルート定義ではコントローラーに UserController が適用される

    $r->get()->path('/users')->action('index');
    $r->post()->path('/users')->action('store');

    $r->path('/user/{id}')->group(function (RouteCollector $r) {
        // このブロックのルート定義ではパスの先頭に /user/{id} が追加される

        // /user/{id}
        $r->get()->action('show');

        // /user/{id}/edit
        $r->get()->path('/edit')->action('edit');
    });
});

// MyRouteCollector.php

/**
 * @method $this controller(string $controller)
 * @method $this action(string $action)
 */
class MyRouteCollector extends RouteCollector {}

// routes.php

$registry = new RouteRegistry();
$r = new MyRouteCollector($registry);

// MyRouteCollector.php

class ActionRouteCollector extends RouteCollector
{
    /**
     * @param string $controller
     * @return static
     */
    public function controller($controller)
    {
        return $this->param(__FUNCTION__, $controller);
    }
}