现在我知道我有以application_frontend 和application_backend 开头的系统路由器。
app/config/routing.yml
application_frontend:
resource: "@ApplicationFrontendBundle/Controller"
prefix: /
type: annotation
application_backend:
resource: "@ApplicationBackendBundle/Controller"
prefix: /backend
type: annotation
我将控制器用作服务,但您不必这样做。
controller.yml
services:
application_frontend.controller.profile:
class: Application\FrontendBundle\Controller\ProfileController
arguments:
- @router
- @doctrine_common_inflector
doctrine_common_inflector:
class: Doctrine\Common\Inflector\Inflector
您应该重构explode 位并进行一些清理,例如从路由名称中删除action。我把它弄脏了,让你看看我到底在做什么。根据您的应用程序,此代码可能会更改。它只是让您了解如何完成。
配置文件控制器
use Doctrine\Common\Inflector\Inflector;
use Symfony\Component\Routing\RouterInterface;
# .......
/**
* @Route("profile", service="application_frontend.controller.profile")
*/
class ProfileController extends Controller
{
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));
}
}
}
}
echo '<pre>';
print_r($list);
exit;
}
}
您可以将列表与app/console debug:router 命令进行比较。
结果
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
)