4

I have a long-term path problem while developing modules.

For instance I have the following project path http://localhost:8888/mylocalproject, which (on the production server) would be http://www.project.com.

Now each time I use global $base_url or other absolute paths it only returns me the http://localhost:8888/ not http://localhost:8888/mylocalproject. For instance if I want to handcraft a logout link will give me http://localhost:8888/user/logout which is wrong, since mylocalproject is excluded.

I have all the time to hack solutions like this

global $base_url;
$needle = "localhost";
if (strpos($_SERVER['REQUEST_URI'], $needle)) {
  path = $base_url."/mylocalproject";
}
else {
  path = $base_url;
}

Is there a Drupal standard solution for that?

5

You should just set the $base_url in your settings file while you're working on localhost, then you can just change it when you put the site live.

The settings file is at "sites/default/settings.php" and you should add:

$base_url = 'http://localhost:8888/mylocalproject';

Obviously when the site goes live you want to change/remove that line to match your new environment.

| improve this answer | |
  • 1
    or better ? global $base_url; $needle = "localhost"; if (strpos($_SERVER['REQUEST_URI'], $needle)) { //$path = $base_url."/psaldo"; $path = 'localhost:8888/psaldo'; } else { $path = $base_url; } – drupal_stuff_alter Feb 15 '12 at 14:29
  • actually, this didn't work for me. I cleared the cache. links to "/" are still going to localhost:8888. Anything else I need to change? Thanks – uwe Feb 15 '12 at 16:53
  • 1
    @MotoTribe If your links are hard coded as "/" then that's how they'll stay, the $base_url is used by internal functions to construct URLs, it doesn't do any sort of replacement on hard-coded URLs used in templates/content/etc. If you're hard coding links and want to use $base_url they would look like this: $link = $base_url . '/a-link.html'. Even better though, use the l() or url() functions as @MPD has suggested as this will take care of everything for you – Clive Feb 15 '12 at 18:06
  • 1
    If you can get a copy of MAMP pro you can have as many sites (virtual hosts) as you want with hostnames like "mysite.local". It works a treat for me – Clive Feb 15 '12 at 20:54
  • 1
    @MotoTribe look on my first comment: use strpos php function to detect the word localhost in $_SERVER['REQUEST_URI'] and if there is, alter the path variable. – drupal_stuff_alter Feb 17 '12 at 12:23
2

You should really use url() and l() as much as humanly possible. If you look at the source, you will see the use of base_path to tack on the proper prefix for where you have Drupal installed. This does assume you have $basepath set properly, which should include the sub-directory (as needed) and not just the hostname/portnumber. A bonus with this is that it takes aliases into account, do you don't need to do gloabl search/replace in code when you change the URL for something.

EDIT TO ADDRESS COMMENT:

$link = l("Add Content", "node/add", array("attributes" => array("class" => "shlinks")));
$shortcut_set['my_list'][] = array("#markup" => "<li>" . $link . "</li>");
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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