i try setup custom router loader here
http://symfony.com/doc/current/cookbook/routing/custom_route_loader.html
i want add dynamic parameter routes (parameter session)
this code
namespace mea\crm4bundle\routing; use mea\crm4bundle\entity\application; use symfony\component\config\filelocatorinterface; use symfony\component\config\loader\loader; use symfony\component\httpfoundation\session\session; use symfony\component\routing\loader\annotationclassloader; use symfony\component\routing\loader\annotationdirectoryloader; use symfony\component\routing\loader\annotationfileloader; use symfony\component\routing\loader\yamlfileloader; use symfony\component\routing\route; use symfony\component\routing\routecollection; use symfony\component\yaml\exception\parseexception; use symfony\component\yaml\parser yamlparser; use symfony\component\config\resource\fileresource; class apploader extends yamlfileloader { private static $availablekeys = array( 'resource', 'type', 'prefix', 'pattern', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', ); private $yamlparser; private $loaded = false; /** * @var */ private $annotationclassloader; /** * @var */ private $session; /** * constructor. * * @param filelocatorinterface $locator filelocatorinterface instance * @param annotationclassloader $annotationclassloader * @param session $session */ public function __construct(filelocatorinterface $locator, annotationclassloader $annotationclassloader, session $session) { $this->locator = $locator; $this->annotationclassloader = $annotationclassloader; $this->session = $session; } public function load($file, $type = null) { $appid = $this->session->get( application::crm_app_id_session ); $path = $this->locator->locate($file); if (!stream_is_local($path)) { throw new \invalidargumentexception(sprintf('this not local file "%s".', $path)); } if (!file_exists($path)) { throw new \invalidargumentexception(sprintf('file "%s" not found.', $path)); } if (null === $this->yamlparser) { $this->yamlparser = new yamlparser(); } try { $parsedconfig = $this->yamlparser->parse(file_get_contents($path)); } catch (parseexception $e) { throw new \invalidargumentexception(sprintf('the file "%s" not contain valid yaml.', $path), 0, $e); } $collection = new routecollection(); $collection->addresource(new fileresource($path)); // empty file if (null === $parsedconfig) { return $collection; } // not array if (!is_array($parsedconfig)) { throw new \invalidargumentexception(sprintf('the file "%s" must contain yaml array.', $path)); } foreach ($parsedconfig $name => $config) { $config['defaults']['_applicationid']=$appid; if (isset($config['pattern'])) { if (isset($config['path'])) { throw new \invalidargumentexception(sprintf('the file "%s" cannot define both "path" , "pattern" attribute. use "path".', $path)); } @trigger_error(sprintf('the "pattern" option in file "%s" deprecated since version 2.2 , removed in 3.0. use "path" option in route definition instead.', $path), e_user_deprecated); $config['path'] = $config['pattern']; unset($config['pattern']); } $this->validate($config, $name, $path); if (isset($config['resource'])) { $this->parseimport($collection, $config, $path, $file); } else { $this->parseroute($collection, $name, $config, $path); } } return $collection; } public function supports($resource, $type = null) { return 'crmapp' === $type; } }
here routing.yml
mea_crm: resource: @meacrm4bundle/resources/config/routing.yml type: crmapp prefix: /{_applicationid} defaults: { _applicationid: 5 }
here services.yml
app.routing_loader: class: mea\crm4bundle\routing\apploader arguments: [@file_locator, @sensio_framework_extra.routing.loader.annot_class,@session] tags: - { name: routing.loader }
this loader fired once if remove cache. think not need. how can override router or in other way - want setup _aplication id default value session.
update 1
i setup router
here working method
class routegenerator extends urlgenerator public function dogenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referencetype, $hosttokens, array $requiredschemes = array()){ if(!isset($parameters[meacrm4bundle::crm_app_switch_parameter])){ //for test $parameters[meacrm4bundle::crm_app_switch_parameter] = 1212; } $url = parent::dogenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referencetype, $hosttokens, $requiredschemes); return $url; }
when add parameters.yml
router.options.generator_base_class: mea\crm4bundle\routing\routegenerator
i have want - twig, controlerrs use router , setup parameter. 1 - not service dont have @session best method session here ?
thy create other class - service ? example override twig generator there exist way override main router service not class ?
update 2
so override router - dont have session - need here
public function __construct(routecollection $routes, requestcontext $context, loggerinterface $logger = null, session $session) { $this->routes = $routes; $this->context = $context; $this->logger = $logger; $this->session = $session; }
but - how session here ?
i guess should try implement symfony\component\routing\generator\urlgeneratorinterface
.
or try extends router , register service.
then need create appropriate extension following 1 provided twigbundle
:
<service id="twig.extension.routing" class="symfony\bridge\twig\extension\routingextension" public="false"> <argument type="service" id="router" /> </service>
to pass custom generator:
<service id="app.twig.url_generator" class="appbundle\twig\extension\routingextension" public="false"> <argument type="service" id="app.url_generator"> </service> <service id="app.url_generator" class="appbundle\routing\appurlgenerator" public="false"> <argument type="service" id="router"> <argument type="service" id="session"> </service>
you need decorate router load route collection.
and don't forget register twig extension :)
Comments
Post a Comment