1

I have a multi-site; each site has its own database but they all share the same codebase. I would like to know if there is a way to define hook_menu() paths at specific base URLs.

Being it a multisite, I have created a common module used by all the sites. I wanted something like this in hook_menu().

 if (base_url == 'wmc.en') {
   $items['wmc/contact-us'] = array('hook menu stuffs');   
 }
 if (base_url == 'gmc.es') {
   $items['gmc/contact-us'] = array('hook menu stuffs');   
 }

I tried this type of code, but hook_menu() doesn't seem to recognize base_url or $_SERVER['SERVER_NAME'].

Is it possible to achieve what I am trying to do?

2 Answers 2

0

In hook_menu() you can access $base_url (not base_url), if you first define it as global (i.e. putting global $base_url as first line of the function) or you access it as $GLOBALS['base_url'].

Keep in mind that $base_url doesn't just contain the domain name, but its content is similar to 'http://example.com/directory_where_drupal_is_installed/'. If you want the domain name you get it with parse_url($GLOBALS['base_url'], PHP_URL_HOST).

That said, I would not use wmc.en/wmc/contact-us and gmc.es/gmc/contact-us as routes, since the domain name makes already the difference between a route and the other one. I would rather use the domain name to execute different code.

if (parse_url($GLOBALS['base_url'], PHP_URL_HOST) == 'wmc.en') {
  // Show the contact form for this domain.
}
elseif (parse_url($GLOBALS['base_url'], PHP_URL_HOST) == 'gmc.es') {
  // Show the contact form for this domain.
}
1
  • Thank you @kiamlalunao. Your solution seems to be the efficient so far. It is working. Oct 24, 2017 at 10:24
0

If you want to use base url, you should first declare it as a global variable and then use it as $base_url. You should also note that $base_url will include the url scheme (i.e) http or https.

If you have a development or staging environment setup, your base url will probably differ between dev, stg and prod environments. In most of the cases, the production environment will be SSL enabled. So you may have to consider this as well while writing code.

In my opinion, as you have mentioned, you may use a different module and enable it only for the specific site. Or if you deliberately want it to be in a common module, consider using access arguments and define hook_permissions.

1
  • Yes I think I should use it as a site specific module as using $base_url conditions does not affect defining hook_menu items. Oct 23, 2017 at 14:26

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.