Skip to content

Instantly share code, notes, and snippets.

@prashantdsala
Created March 6, 2023 10:44
Show Gist options
  • Save prashantdsala/83d4d5fb8e28805d4de551889855dd54 to your computer and use it in GitHub Desktop.
Save prashantdsala/83d4d5fb8e28805d4de551889855dd54 to your computer and use it in GitHub Desktop.
How to write a custom Controller in Drupal
custom_controller.example:
path: '/custom-controller/example'
defaults:
_title: 'Example'
_controller: '\Drupal\custom_controller\Controller\CustomControllerController::build'
requirements:
_permission: 'access content'
<?php
namespace Drupal\custom_controller\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Returns responses for Custom Controller routes.
*/
class CustomControllerController extends ControllerBase {
/**
* Builds the response.
*/
public function build() {
$build['content'] = [
'#type' => 'item',
'#markup' => $this->t('It works!'),
];
return $build;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment