PHP code example of taro / php-router

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

    

taro / php-router example snippets


// メソッド名('URLのパス', '登録したい値', [オプション配列])
$router->post('/products', 'Products@store');

// パラメータは :パラメータ名 で指定
$router->put('/products/:id', 'Products@update');
$router->delete('/products/:id', 'Products@delete');

// 第3引数のオプションにmiddlewareを指定
$router->get('/products', 'Products@index', ['middleware'=>['auth','api']]);

// パラメータ名の末尾に ? を付けると、オプションになる
$router->get('/users/:id?', 'routeWithOptionParam');

$router->controller('/tasks', 'TasksController');
// 以下のルートを登録
// GET     /tasks              TasksController@index
// GET     /tasks/:tasks       TasksController@show
// GET     /tasks/create       TasksController@create
// GET     /tasks/:tasks/edit  TasksController@edit
// POST    /tasks              TasksController@store
// PUT     /tasks/:tasks       TasksController@update
// DELETE  /tasks/:tasks       TasksController@delete    

$router->setRoutes([
    ["GET","/" , 'index.html'],
    ["GET","/etc/php5/abc/:role" , "role.php"],
    ["GET","/etc/php5/cli/man/readme" , "[email protected]", ['middleware'=>'auth']],
]);

// group([グループパラメータ], function($router){  ここにルート定義文を記述   })
$router->group(['prefix'=>'order/:order_no','middleware'=>'admin'], function ($router) {
    $router->get('/shipping', 'Shipping@index'); // url => order/:order_no/shipping

    // group は入れ子にできる
    $router->group(['prefix'=>'payment'], function ($router) {
      $router->delete('/credit/:code', 'Credit@delete'); // url => order/:order_no/payment/credit/:code
    });
});

$router = new Router();
$router->loadRoutes([routes.phpへのパス]);
// $router->showTrees(); 作成したルーティング木を表示

$url = '/products';
$result = $router->match($url, 'GET');

print_r($result);

[
    'callback'  => '登録されている値',
    'url' => $url,
    'params' => [
        'id' => 111, // ルートパラメータがある時、パラメーター名をキーとした配列を返す
    ]
    'options' => [
        'middleware'=>['web']
    ]
]

$router->showTable();

// ルーティング表表示
|Method |Url                                           |Action                    |Options
|DELETE |/tasks/:tasks                                 |TaskController@delete     |middleware=web
|   GET |/tasks/:tasks/edit                            |TaskController@edit       |middleware=web
|   GET |/tasks/:tasks                                 |TaskController@show       |middleware=web
|   GET |/tasks/create                                 |TaskController@create     |middleware=web
|   GET |/tasks                                        |TaskController@index      |middleware=web
|  POST |/tasks                                        |TaskController@store      |middleware=web
|   PUT |/tasks/:tasks                                 |TaskController@update     |middleware=web