2

I have a route with optional parameter that has some default value:

# file @hello.routing.yml
hello.sayHi:
  path: '/hello/{name}'
  defaults: 
    _title: 'Say Hi'
    _content: 'Drupal\hello\Controller\SimpleCallback::sayHiAction'
    name: 'there'
  requirements:
    _permission: 'access content'

I'd like to make there value for {name} parameter configurable. Let's assume that this default value for {name} is available in simple config variable.

6
  • Have you tried adding a default value to the controller method (sayHiAction)?
    – Eyal
    Jan 19, 2016 at 15:01
  • Remove the name form route and use it in controller. why are you trying to complicate things for yourself? :D
    – user21641
    Jan 19, 2016 at 17:02
  • @IvanJaros because I want to go by the book, be consistent and maybe use parameter upcasting? Jan 19, 2016 at 18:52
  • Either you haven't explained what you want correctly, or I didn't understood what you want or you don't understand what "I want to go by the book" means :D .. as I'm reading the answer from cilefen I think you might want to have a look at \Drupal\Core\Routing\RouteSubscriberBase
    – user21641
    Jan 19, 2016 at 19:56
  • OK, so symfony allows you to use config parameter values in yaml file by referencing them with a % sign. For example: stackoverflow.com/a/18782406 I'm looking for similar behavior that will work for Drupal's simple config. BTW it would be awesome if we could use simple config variables in yaml files like we can with symfonys native config parameters. "By the book" I mean the best practice approach, like with symfonys service: you don't use default value in a service constructor but instead you inject the value in your service definition. Jan 19, 2016 at 21:17

3 Answers 3

2

Implement two routes one with an argument and the second without.

hello.routing.yml

hello.sayHi:
  path: '/hello/{name}'
  defaults: 
    _title: 'Say Hi'
    _content: 'Drupal\hello\Controller\SimpleCallback::sayHiAction'
  requirements:
    _permission: 'access content'
hello.sayHi.default:
  path: '/hello'
  defaults: 
    _title: 'Say Hi'
    _content: 'Drupal\hello\Controller\SimpleCallback::sayHiAction'
  requirements:
    _permission: 'access content'

hello.config.yml

default_name: there

hello.services.yml

hello.config:
  factory: ['@config.factory', get]
  class: Drupal\Core\Config\Config
  arguments: ['hello.config']

Drupal\hello\Controller\SimpleCallback

protected $configuration;

public function __constructor(Config $configuration) {
  $this->configuration = $configuration;
}

public static function create(Container $container) {
  return new static($container->get('hello.config'));
}

public function sayHiAction($name = NULL) {
  $name = is_null($name) ? $this->configuration->get('default_name') : $name;
  return [
    '#type' => 'inline_template',
    '#template' => 'Hi {name}',
    '#context' => [
      'name' => $name
    ]
  ]
}
1

You must implement a route alter. In the alter code, extract the setting and set the value on the route definition.

2
  • This looks promissing. I'll give it a shoot. I'd be happy to accept your answer, but can you update it with code example just for sake of stackexchange rules? Jan 19, 2016 at 18:57
  • No. SE has their rules and I have mine. It is well-documented at the link.
    – cilefen
    Jan 19, 2016 at 21:56
-2

https://www.drupal.org/docs/8/api/routing-system/parameters-in-routes/using-parameters-in-routes

Optional parameters

Parameters on routes can be omitted when a default value for the parameter is supplied. Imagine you have a form controller that allows people to report different issues (e.g. bug reports, feature requests and support requests), and if the type is omitted it should default to 'support request'. Supply the default value for the optional parameter in the "defaults" section:

issue.report_form:
  path: '/report/{issue_type}'
  defaults: 
    _controller: '\Drupal\issue\Controller\IssueController::report'
    issue_type: 'support-request'
  requirements: 
    _permission: 'report issue' 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.