问题
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
# 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\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/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
I'm confused about the line
alias for "router.default"
I can't find any documentation on this.
It seems something has changed in Symfony, but I can't find what
回答1:
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
and class
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;
}
}
回答2:
router.class i think in older symfoy version use router.options.generator_class instead
来源:https://stackoverflow.com/questions/46021326/symfony-3-3-custom-router