First time I am creating a custom module in Drupal 8. I have some pages having only HTML markup and JS code.
Assume I have 3 pages flow and user goes to 1st page (next button) -> 2nd page (next button) -> 3rd page.
To achieve it I have created a custom module as below. My custom module requires multiple routes with its own separate twig/view template.
The name of my module is custom_demo and it is created at /web/modules/custom/custom_demo with necessary artefacts as below.
The issue is first 2 pages work, the moment I click on next button to go on 3rd page, it does not work and shows The requested page could not be found. message.
Working
http://mysite.dd:8083/mypage/page1
http://mysite.dd:8083/mypage/page2
Not Working
http://mysite.dd:8083/mypage/page3
custom_demo.info.yml
name: Demo
type: module
description: 'Demo Description'
package: Custom
core: 8.x
custom_demo.routing.yml
custom_demo.page1:
path: '/mypage/page1'
defaults:
_controller:
'\Drupal\custom_demo\Controller\DemoController::page1'
_title: 'test title1'
requirements:
_permission: 'access content'
custom_demo.page2:
path: '/mypage/page2'
defaults:
_controller: '\Drupal\custom_demo\Controller\DemoController::page2'
_title: 'test title2'
requirements:
_permission: 'access content'
custom_demo.page3:
path: '/mypage/page3'
defaults:
_controller: '\Drupal\custom_demo\Controller\DemoController::page3'
_title: 'test title3'
requirements:
_permission: 'access content'
custom_demo.module
<?php
function custom_demo_theme($existing, $type, $theme, $path) {
return array(
'demo_page1' => array(
'variables' => array('title' => NULL),
'template' => 'demo-page1',
),
'demo_page2' => array(
'variables' => array('title' => NULL),
'template' => 'demo-page2',
),
'demo_page3' => array(
'variables' => array('title' => NULL),
'template' => 'demo-page3',
),
);
}
/src/Controller/DemoController.php
<?php
namespace Drupal\custom_demo\Controller;
use Drupal\Core\Controller\ControllerBase;
class DemoController extends ControllerBase {
public function page1() {
return array(
'#theme' => 'demo_page1',
'#title' => 'test title1',
);
}
public function page2() {
return array(
'#theme' => 'demo_page2',
'#title' => 'test title2',
);
}
public function page3() {
return array(
'#theme' => 'demo_page3',
'#title' => 'test title3',
);
}
}
Twig templates
Under custom module folder I created the folder /templates and created following 3 files with some test content.
- demo-page1.html.twig
- demo-page2.html.twig
- demo-page3.html.twig
Can someone please help, why the http://mysite.dd:8083/mypage/page3 is not woking?
Thanks - Hitesh