In Drupal 7, to declare path and its options, we need to implement hook_menu().
Drupal 8, create routing file in the top level module directory. If i need to create a custom path, then in custom module (myfirstcustommodule) directory create a file <modulename>.routing.yml.
"hook_menu()" needs "page-callback" function while in Drupal 8, page callback must be either a class or a registered service.
Drupal 8, create routing file in the top level module directory. If i need to create a custom path, then in custom module (myfirstcustommodule) directory create a file <modulename>.routing.yml.
"hook_menu()" needs "page-callback" function while in Drupal 8, page callback must be either a class or a registered service.
Drupal 7 hook_menu
<?php
/**
* Implementation of hook_menu
*/
function firstcustommodule_menu() {
$items = array();
$items['company/list'] = array(
'title' => t('All Companies'),
'page callback' => 'display_company_listing',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_menu
*/
function firstcustommodule_menu() {
$items = array();
$items['company/list'] = array(
'title' => t('All Companies'),
'page callback' => 'display_company_listing',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
Drupal 8
File: firstcustommodule.routing.yml
firstcustommodule.company_list:
path: '/company/list'
defaults:
_controller: '\Drupal\firstcustommodule\Controller\CompanyController::getCompanyList'
_title: 'Company Listing'
requirements:
_permission: 'access content'
firstcustommodule.company_list: It is route machine name. Machine name should me modulename.subname
path: It is the path to the page on site. It should have leading slash(/).
defaults: It describes page and title callbacks.
requirements: This having conditions under which page will be displayed. It should specify permissions, modules(on which this depends), and other conditions.
Now controller class CompanyController should be in firstcustommodule\src\Controller\CompanyController\CompanyController.php :
<?php
/**
* @file
* Contains \Drupal\firstcustommodule\Controller\CompanyController
*/
namespace Drupal\firstcustommodule\Controller;
use Drupal\Core\Controller\ControllerBase;
class CompanyController extends ControllerBase {
public function getCompanyList() {
return [
'#type' => 'markup',
'#markup' => $this->t('This is company listing page by CompanyController with routing file.'),
];
}
}
Save above created file.
No comments:
Post a Comment