How do I programmatically create a page in drupal 8?

3
(1)
// src/Controller/ModulenameController.php
 
namespace Drupal\modulename\Controller;
 
use Drupal\Core\Controller\ControllerBase;
 
class ModulenameController extends ControllerBase {
  public function helloWorld() {
    return [
      '#markup' => 'Hello, World!',
    ];
  }
}
# modulename.routing.yml
 
modulename.hello_world:
  path: '/hello/world'
  defaults:
    _controller: '\Drupal\modulename\Controller\ModulenameController::helloWorld'
    _title: 'Hello, World!'
  requirements:
    _permission: 'access content'

Or using drush:

# vendor/bin/drush generate controller
61

How useful was this post?

Click on a star to rate it!

Average rating 3 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

Scroll to Top