2016-11-09 6 views
1

スリムフレームワークで定義されたルートから動的なドロップダウンメニューを作成しようとしています。ここに私の質問があります。何らかの種類の配列から定義されたすべての静的ルートにアクセスする方法はありますか?例えばスリム3のPHPフレームワークからすべてのルートにアクセスするには?

私はこのような私のルートを定義した場合、:

// Index page: '/' 
require_once('pages/main.php'); 

// Example page: '/hello' 
require_once('pages/hello.php'); 

// Example page: '/hello/world' 
require_once('pages/hello/world.php'); 

// Contact page: '/contact' 
require_once('pages/contact.php'); 

各ファイルには、私はからこれらの定義されたすべてのルートにアクセスしたいと思い、この

// Index page 
$app->get('/', function ($request, $response, $args) { 

    // Some code 

})->setName('index'); 

のように見える別のページですいくつかの配列の並べ替えを使用し、その配列を使用して、テンプレートファイル内の順不同のHTMLリストを作成します。

<ul> 
    <li><a href="/">Index</a></li> 
    <li><a href="/hello">Hello</a> 
    <ul> 
     <li><a href="/hello/world">World</a></li> 
    </ul> 
    </li> 
    <li><a href="/contact">Contact</a></li> 
</ul> 

定義されたルートを変更するときはいつでも、このメニューを変更したいと思います。これを達成する方法はありますか?

答えて

5

Router class in GitHub project for Slimのクイック検索では、$this->routes[]ルートオブジェクトの配列を返すパブリックメソッドgetRoutes()が表示されます。ルートオブジェクトから、あなたがgetPattern()メソッドを使用して、ルートパターンを得ることができます。

$routes = $app->getContainer()->router->getRoutes(); 
// And then iterate over $routes 

foreach ($routes as $route) { 
    echo $route->getPattern(), "<br>"; 
} 

編集を:追加されました例

+0

しかし、これはおそらく、OPが必要としない 'Route' [objects](https://github.com/slimphp/Slim/blob/3.x/Slim/Route.php)の束を返します。 –

+1

@GeorgyIvanovはい、配列を反復処理するときに、印刷する各オブジェクト内のプロパティを選択することができます。 – Wolf

+0

これは私の望むように動作するようです。おかげでウルフ!すべてのページが 'require_once'でロードされた後、PHPファイルに$ container ['allRoutes'] = $ app-> getContainer() - > router-> getRoutes();'を入れて使用するようにしました'$ routes = $ this-> allRoutes;'ページ内から。このアプローチは大丈夫ですか? –

2

はい。あなたはSlimのname your routesです。これは非常に単純な方法で行われます。

$app->get('/hello', function($request, $response) { 
    // Processing request... 
})->setName('helloPage'); // setting unique name for route 

今、あなたは、このような名前でURLを取得できます。

$url = $app->getContainer()->get('router')->pathFor('helloPage'); 
echo $url; // Outputs '/hello' 

あなたはあまりにもプレースホルダをルートに名前を付けることができます。

// Let's define route with placeholder 
$app->get('/user-profile/{userId:[0-9]+}', function($request, $response) { 
    // Processing request... 
})->setName('userProfilePage'); 

// Now get the url. Note that we're passing an array with placeholder and its value to `pathFor` 
$url = $app->getContainer()->get('router')->pathFor('helloPage', ['userId': 123]); 
echo $url; // Outputs '/user-profile/123' 

これをテンプレート内の名前でルートを参照する場合は、URLを変更する必要がある場合は、ルートの定義でのみ行う必要があるため、移動する方法は間違いありません。私はこれを特に涼しい見つける。

0

ウルフの回答に基づいて、私は私の解決策を行っています。

ルートを定義するページでは、すべてのルートの下に定義されたルートを持つコンテナを作成します。

// Get ALL routes 
// -------------- 
$allRoutes = []; 
$routes = $app->getContainer()->router->getRoutes(); 

foreach ($routes as $route) { 
    array_push($allRoutes, $route->getPattern()); 
} 

$container['allRoutes'] = $allRoutes; 

これは、後でプロジェクトで使用できる必要なルートを持つ配列を作成します。私たちは私たちが望むものは何でも作成するには、この配列を使用できるように

$app->get('/helloWorld', function ($request, $response, $args) { 

    print_r ($this->allRoutes); 

})->setName('helloWorld'); 

この意志出力

Array 
(
    [0] =>/
    [1] => /helloWorld 
    [2] => /someOtherRoute 
    ... 
) 

は、その後、あなたの定義されたルートの中から、あなたはこのように、これを呼び出すことができます。おかげで再び狼!

+0

プレースホルダで動作しますか? –

+1

それはできません。静的ルートのみ。 –

+0

'$ route-> getMethods()'のデータを取得してルートが 'GET' /' POST'なのかどうかを知ることも価値があります – user3791372

関連する問題