【问题标题】:Get all URL used by routes in Symfony 2获取 Symfony 2 中路由使用的所有 URL
【发布时间】:2015-08-23 15:58:19
【问题描述】:

我正在使用 Symfony 2 开发一个网站,其中每个用户都有一个可通过domain.com/username 访问的个人资料页面(就像许多社交网站一样)。

为了实现这一点,我需要检查用户想要选择的 URL 是否可用。 我想知道 Symfony 中是否有办法让路由配置中定义的路由使用的所有 URL 自动禁止这些路由作为用户名。

谢谢

【问题讨论】:

  • 您可以通过container->get('router')container获取Route的实例。从那里您可以访问一个名为getRouteCollection() 的方法,该方法将返回RouteCollection 的一个实例,并从那里调用方法all(),该方法将返回每个路由的数组。
  • 完美运行!非常感谢!
  • @ibi0tux,根据你的情况,用户名和路由是1:1的关系。如果我没记错的话,只要用户名在你的表中是唯一的,那么路由就永远是唯一的。
  • 我的问题不是让每个用户名都唯一,而是阻止用户选择与我的路由配置(网站的其他页面)中的路径匹配的用户名
  • 另一种方法是获取 url 匹配器服务,然后简单地执行 $matcher->match('/' . $proposedUsername)。如果匹配,则名称无效。

标签: php symfony url routing


【解决方案1】:

现在我知道我有以application_frontendapplication_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
)

【讨论】:

    猜你喜欢
    • 2016-04-12
    • 2016-03-29
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    相关资源
    最近更新 更多