Created
March 6, 2023 10:44
-
-
Save prashantdsala/83d4d5fb8e28805d4de551889855dd54 to your computer and use it in GitHub Desktop.
How to write a custom Controller in Drupal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
custom_controller.example: | |
path: '/custom-controller/example' | |
defaults: | |
_title: 'Example' | |
_controller: '\Drupal\custom_controller\Controller\CustomControllerController::build' | |
requirements: | |
_permission: 'access content' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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