我有一个应用程序,可通过登录进行双重访问,并使用相同路径的OAuth客户端密钥。对于Oauth访问,我需要传递一个url参数:" access_token"在所有网址上。
最好使用自定义路由器实现此目的:
应用程序/配置/ services.yml
\g<0>
的appbundle \路由\ AccessTokenRouter
# 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"]
我没有错误,但永远不会调用自定义路由器。
另外,当我调试路由时:
bin / console debug:container | grep rout
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;
}
}
我对这条线感到困惑
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
我无法找到相关文档。
Symfony似乎发生了一些变化,但我无法找到
答案 0 :(得分:3)
您确定参数router.class
吗?我没有找到这样的参数...
尝试制作自定义网址生成器
<强>配置强>
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;
}
}
答案 1 :(得分:1)
router.class我认为在较旧的symfoy版本中使用router.options.generator_class而不是