1

I have created a routing URL like

login.compnay_login_form:
  path: '/u/{company_name}/login'
  defaults:
    _controller: '\Drupal\login\Controller\LoginController::login_form'
    _title: 'Login'
  requirements:
    _permission: 'access content

This works with http://example.com/u/testcompany/login

But I want the URL like http://example.com/testcompany/login for that I write the below routing

login.compnay_login_form:
  path: '/{company_name}/login'
  defaults:
    _controller: '\Drupal\login\Controller\LoginController::login_form'
    _title: 'Login'
  requirements:
    _permission: 'access content'

but it's not working.

Please let me know if any other way to achieve this.

http://example.com/testcompany/login

should call to

http://example.com/u/testcompany/login

may be through htaccess or any other way?

2
  • did you rebuild the cache?
    – Yuseferi
    Oct 4, 2016 at 8:37
  • 1
    yes,actually I think drupal 8 not support to URL routing with the first parameter as a dynamic parameter. if I used with just one character (u) all is working fine as I have added first routing Oct 4, 2016 at 8:55

1 Answer 1

1

If there is a node for each company, you can insert an alias when the node is saved. Set up the alias created for each company to start with '/company/{company_name}', then when the alias is inserted, you can add a secondary alias for login:

/**
 * Implements hook__path_insert().
 */
function mymodule_path_insert($path) {
  if (preg_match('/company\/(.*)/', '/company/xyz', $matches) && preg_match('/node\/(\d+)$/', $path['source'])) {
  $company_name = $matches[1];
    \Drupal::service('path.alias_storage')->save("/u/$company_name/login", "$company_name/login" $path['langcode']);
  }
}

This way, the visible path will be /{company_name}/login, but the machine name as far as Drupal is concerned with be /u/{company_name}/login.

You could instead do this on entity_insert and entity_update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.