Adding parameters to route in Drupal 8

posted in: Drupal | 0

To pass parameters to controller using URI is possible in Drupal 8. It makes URI to contain parameters that can be processed in the controller.Parameters are defined in the routing.yml with curly braces.Following is the example code to pass parameters to route in routing.yml file.

hello_world.hello_route:
path: 'hello/{route}'
defaults:
    _controller: Drupal\hello_world\Controller\HelloController::hello_route
    _title: 'This is title with parameter in router'
requirements:
    _permission: 'access content'

 

The controller takes it as a parameter to its function and it gets processed

namespace Drupal\hello_world\Controller;

use Drupal\Core\Controller\ControllerBase;

class HelloController extends ControllerBase {
        
    public function hello_route($route){
        return [
            '#type' => 'markup',
            '#markup' => 'This is page with parameter = '.$route 
        ];
    }

}

 

Leave a Reply