Create a custom page with arguments

Tutorial Goal

Lesson Goal:

Implementation Method

D7

D8

Use hook_menu()

Use lotus.routing.yml file

D7 Code

Add the following in lotus.module file:

/**
* Implements hook_menu()
**/

function lotus_menu() {
    $items['offers/hot/%'] = array(
      'title' => 'Offers Galore!',
      'description' => 'Landing Page for Offers Page',
      'page callback' => 'lotus_offers_page',
      'page arguments' => array(1),
      'access arguments' => array('access content')
    );
    return $items;
  }

/** 
* Page callback function
**/

  function 'lotus_offers_page'($count) {
    return 'You will get ' . $count . '% discount!!' ;
  }

D8 Code

1. Add the following in lotus.routing.yml file

lotus.offers_controller_hello:
  path: '/offers/hot/{count}'
  defaults:
    _controller: '\Drupal\lotus\Controller\OffersController::hello'
    _title: 'Offers'
  requirements:
    _permission: 'access content'

2. Add the following in lotus/src/Controller/OffersController.php file

namespace Drupal\lotus\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Class OffersController.
 *
 * @package Drupal\lotus\Controller
 */
class OffersController extends ControllerBase {

  /**
   * Hello.
   *
   * @return string
   *   Return Hello string.
   */
  public function hello($count) {
    return [
      '#type' => 'markup',
      '#markup' => $this->t('You will get %count% discount!!', array('%count' => $count)),
    ];
  }

}

Gist Link

https://gist.github.com/gargsuchi/f1e7276f26c7d152f74b295ca586df73

 

Contact supportStill need assistance? Contact Acquia Support