Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取Symfony 2中路由使用的所有URL_Php_Symfony_Url_Routing - Fatal编程技术网

Php 获取Symfony 2中路由使用的所有URL

Php 获取Symfony 2中路由使用的所有URL,php,symfony,url,routing,Php,Symfony,Url,Routing,我正在使用Symfony 2开发一个网站,每个用户都可以在domain.com/username上访问一个个人资料页面(就像许多社交网站提供的那样) 为了实现这一点,我需要检查当用户想要选择一个URL时,想要的URL是否可用。 我想知道在Symfony中是否有一种方法可以获取路由配置中定义的路由使用的所有URL,从而自动禁止这些路由作为用户名 谢谢现在我知道我有以应用程序前端和应用程序后端开始的系统路由器 app/config/routing.yml application_frontend:

我正在使用Symfony 2开发一个网站,每个用户都可以在
domain.com/username
上访问一个个人资料页面(就像许多社交网站提供的那样)

为了实现这一点,我需要检查当用户想要选择一个URL时,想要的URL是否可用。 我想知道在Symfony中是否有一种方法可以获取路由配置中定义的路由使用的所有URL,从而自动禁止这些路由作为用户名


谢谢

现在我知道我有以
应用程序前端
应用程序后端
开始的系统路由器

app/config/routing.yml

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

application_backend:
    resource: "@ApplicationBackendBundle/Controller"
    prefix:   /backend
    type:     annotation
services:
    application_frontend.controller.profile:
        class: Application\FrontendBundle\Controller\ProfileController
        arguments:
            - @router
            - @doctrine_common_inflector

    doctrine_common_inflector:
        class: Doctrine\Common\Inflector\Inflector
我将我的控制器用作服务,但您不必这样做

controller.yml

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

application_backend:
    resource: "@ApplicationBackendBundle/Controller"
    prefix:   /backend
    type:     annotation
services:
    application_frontend.controller.profile:
        class: Application\FrontendBundle\Controller\ProfileController
        arguments:
            - @router
            - @doctrine_common_inflector

    doctrine_common_inflector:
        class: Doctrine\Common\Inflector\Inflector
您应该重构
分解
位,并进行一些清理,例如从路由名称中删除
操作
。我把它弄得有点脏,让你看看我到底在那里干什么。根据您的应用程序,此代码可能会更改。它只是给你一个如何做的想法

ProfileController

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;
    }
}

您可以使用
container->get('router')
container
获取
Route
的实例。从那里,您可以访问名为
getRouteCollection()
的方法,该方法将返回
RouteCollection
的实例,并从那里调用方法
all()
,该方法将返回每个路由的数组。它工作得非常好!非常感谢@ibi0tux,根据您的情况,用户名和路由具有1:1的关系。如果我没有弄错,那么只要用户名在您的表中是唯一的,那么路由将始终是唯一的。我的问题不是让每个用户名都是唯一的,而是阻止用户选择与我的路由配置(网站的其他页面)中的路径相匹配的用户名另一种方法是获取url匹配器服务,然后只需执行$matcher->match('/'.$proposedUsername)。如果找到匹配项,则名称无效。