Routes & Controllers
# Generate & Register a controller
$ drupal generate:controller [options]
$ gcn
module_name.example_controller:
path: '/custom_path/{event_id}/{price_id}'
defaults:
_controller: '\Drupal\module_name\Controller\ExampleController::conteont'
requirements:
_permission: 'access content'
_role: 'authenticated'
#https://www.drupal.org/docs/8/api/routing-system/introductory-drupal-8-routes-and-controllers-example
<?php
namespace Drupal\example\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* An example controller.
*/
class ExampleController extends ControllerBase {
/**
* {@inheritdoc}
*/
public function content() {
$build = array(
'#type' => 'markup',
'#markup' => t('Hello World!'),
);
return $build;
}
}
// See https://www.drupal.org/docs/8/api/routing-system/introductory-drupal-8-routes-and-controllers-example
You can already have access to several utilities service inside the a controller :
- Current User
- Entity Manager
- URL
- Language
- Etc ...
See https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Controller!ControllerBase.php/class/ControllerBase/8.2.x