symfony 3.3自定义路由器 [英] symfony 3.3 custom router

查看:108
本文介绍了symfony 3.3自定义路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序可以通过登录和具有相同路由的OAuth客户端密码进行双重访问。对于Oauth访问,我需要在所有URL上传递一个url参数: access_token。

I have an application, which is dual accessible through login and an OAuth client secret with the same routes. For Oauth access I need to pass a url parameter: "access_token" around on all urls.

似乎最好使用自定义路由器来实现:

It seems best to achieve this with a custom router:

app / config / services.yml

app/config/services.yml

# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
    router.class: AppBundle\Routing\AccessTokenRouter

services:
    # default configuration for services in *this* file
    _defaults:
        # automatically injects dependencies in your services
        autowire: true
        # automatically registers your services as commands, event subscribers, etc.
        autoconfigure: true
        # this means you cannot fetch services directly from the container via $container->get()
        # if you need to do this, you can override this setting on individual services
        public: false

    # makes classes in src/AppBundle available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    AppBundle\:
        resource: '../../src/AppBundle/*'
        # you can exclude directories or files
        # but if a service is unused, it's removed anyway
        exclude: '../../src/AppBundle/{Entity,Tests}'

    # controllers are imported separately to make sure they're public
    # and have a tag that allows actions to type-hint services
    AppBundle\Controller\:
        resource: '../../src/AppBundle/Controller'
        public: true
        tags: ['controller.service_arguments']

    # add more services, or override services that need manual wiring
    # AppBundle\Service\ExampleService:
    #     arguments:
    #         $someArgument: 'some_value'
    app.access_token_user_provider:
        class: AppBundle\Security\AccessTokenuserProvider
        arguments: ["@doctrine.orm.entity_manager"]

AppBundle\Routing\ TokenAccessTokenRouter

AppBundle\Routing\AccessTokenRouter



use Symfony\Bundle\FrameworkBundle\Routing\Router as BaseRouter;

class AccessTokenRouter extends BaseRouter
{
    public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
    {
        // parent router generates url
        $url = parent::generate($name, $parameters, $referenceType);

        // check for existing preview query string
        parse_str($this->getContext()->getQueryString(), $contextQueryParams);
        if(isset($contextQueryParams['access_token']))
        {
            // put possible query string params into $queryParams array
            $urlParts = parse_url($url);
            parse_str(isset($urlParts['query']) ? $urlParts['query'] : '', $urlQueryParams);

            // strip everything after '?' from generated url
            $url = preg_replace('/\?.*$/', '', $url);

            // append merged query string to generated url
            $url .= '?'.http_build_query(array_merge(
                                             array('access_token' => $contextQueryParams['access_token']),
                                             $urlQueryParams
                                         ));
        }

        return $url;
    }
}

我没有收到任何错误,但是自定义路由器从未

I get no errors, but the custom router is never called.

此外,当我调试路由时:

Also, when I debug routing:

bin /控制台debug:container | grep rout

bin/console debug:container |grep rout

 data_collector.router             Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector                            
 monolog.logger.router             Symfony\Bridge\Monolog\Logger                                                               
 router                            alias for "router.default"                 
 router_listener                   Symfony\Component\HttpKernel\EventListener\RouterListener                                   
 routing.loader                    Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader                                     
 web_profiler.controller.router    Symfony\Bundle\WebProfilerBundle\Controller\RouterController  

我对这行感到困惑

    alias for "router.default" 

我找不到任何文档。

似乎有些东西Symfony中进行了更改,但是我找不到什么

It seems something has changed in Symfony, but I can't find what

推荐答案

您确定参数 router.class吗? ?我找不到这样的参数...

Are you sure about parameter router.class? I didn't find so parameter...

尝试制作自定义网址生成器

Try to make custom url generator

config

parameters:
    router.options.generator_class: AppBundle\Routing\AccessTokenUrlGenerator
    router.options.generator_base_class: AppBundle\Routing\AccessTokenUrlGenerator

并分类

use Symfony\Component\Routing\Generator\UrlGenerator as BaseUrlGenerator ;

    public function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes)
    {
        // parent router generates url
        $url = parent::doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes);

        // check for existing preview query string
        parse_str($this->getContext()->getQueryString(), $contextQueryParams);
        if(isset($contextQueryParams['access_token']))
        {
            // put possible query string params into $queryParams array
            $urlParts = parse_url($url);
            parse_str(isset($urlParts['query']) ? $urlParts['query'] : '', $urlQueryParams);

            // strip everything after '?' from generated url
            $url = preg_replace('/\?.*$/', '', $url);

            // append merged query string to generated url
            $url .= '?'.http_build_query(array_merge(
                                             array('access_token' => $contextQueryParams['access_token']),
                                             $urlQueryParams
                                         ));
        }

        return $url;
    }
}

这篇关于symfony 3.3自定义路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆