Aşağıdaki örnek size bir controller içindeyken sistemdeki tüm routelerin listesini almayı gösterir.


Bundle listesi


Gördüğümüz gibi, routeler application_frontend ve application_backend kelimeleri ile başlıyorlar.


# app/config/routing.yml

application_frontend:
resource: "@ApplicationFrontendBundle/Controller"
prefix: /
type: annotation

application_backend:
resource: "@ApplicationBackendBundle/Controller"
prefix: /backend
type: annotation

Controllers.yml


services:
application_frontend.controller.profile:
class: Application\FrontendBundle\Controller\ProfileController
arguments:
- @router
- @doctrine_common_inflector

doctrine_common_inflector:
class: Doctrine\Common\Inflector\Inflector

Controller


Burada isterseniz biraz temizlik yapabilirsiniz.


use Doctrine\Common\Inflector\Inflector;
use Symfony\Component\Routing\RouterInterface;
# .......

/**
* @Route("profile", service="application_frontend.controller.profile")
*/
class ProfileController
{
private $router;
private $inflector;

public function __construct(
RouterInterface $router,
Inflector $inflector
) {
$this->router = $router;
$this->inflector = $inflector;
}

/**
* @Method({"GET"})
* @Route("/step_one")
*/
public function stepOneAction()
{
$list = [];
$myRoutes = ['application_frontend', 'application_backend'];
$routeCollection = $this->router->getRouteCollection();

foreach ($routeCollection->all() as $key => $value)
{
$data = $value->getDefaults();

if (isset($data['_controller'])) {
foreach ($myRoutes as $routePrefix) {
if (strstr($data['_controller'], $routePrefix) !== false) {
$routeParts = explode(':', $data['_controller']);
$routeParts2 = explode('.', $routeParts[0]);

$list[] = end($routeParts2). '/' . $this->inflector->tableize(end($routeParts));
}
}
}
}

print_r($list);
exit;
}
}

Sonuç


Array
(
[0] => country/index_action
[1] => country/list_action
[2] => default/index_action
[3] => default/list_action
[4] => profile/step_one_action
[5] => profile/step_two_action
[6] => profile/step_one_process_action
[7] => profile/step_two_process_action
[8] => security/index_action
[9] => security/login_action
[10] => security/logout_action
[11] => api/json_action
[12] => api/xml_action
[13] => api/json_xml_action
[14] => default/index_action
[15] => default/list_action
[16] => default/secret_action
[17] => superadmin/index_action
[18] => superadmin/list_action
[19] => twitter/list_action
[20] => twitter/create_tweet_action
[21] => user/list_action
[22] => user/create_action
)