ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Subdomain as global variable

March 02, 2012 12:26pm

Subscribe [3]
  • #1 / Mar 02, 2012 12:26pm

    fitzage

    100 posts

    I’m setting up a multilingual site using the subdomains as the trigger for switching languages.

    I need to save the subdomain as a global variable so I can then use it when determining the language.

    I found this bit of code that looks like it may do the trick, but I have no idea where I would put it.

    $domainChunks = explode('.', $_SERVER['SERVER_NAME']);
    $subDomain = $domainChunks[0];
    $global_vars['language'] = $subDomain;

    Anybody have an answer? Or a better chunk of code?

  • #2 / Mar 02, 2012 12:42pm

    Enviromed

    375 posts

    What EE version? 
    Are you using the classic subdomain method [not EE supported] or MSM?

  • #3 / Mar 02, 2012 12:50pm

    fitzage

    100 posts

    Neither. All subdomains are going to the same site, same templates, everything.

    I need a global variable triggered by the subdomain to then use for accessing my custom fields based on language.

  • #4 / Mar 02, 2012 1:09pm

    fitzage

    100 posts

    Oh, almost forgot: Running EE 2.4, latest build.

  • #5 / Mar 02, 2012 5:02pm

    Enviromed

    375 posts

    I see you commented on Erskine’s EE 1x subdomain article on 06-14-09:

    http://erskinelabs.com/create-an-alternative-version-of-your-expressionengine-website/

    The EE 2 updated reference is [focussed on mobile]

    http://supergeekery.com/geekblog/comments/how_to_make_a_mobile_version_of_your_site_with_expression_engine_2

    Would greatly appreciate it if you could post the code you end up using.  Thanks

  • #6 / Mar 04, 2012 2:24pm

    Dan Decker

    7338 posts

    Hi fitzage,

    How are you setting up the sub-domains? If they are sub-folders of your main domain, you can place a copy of your main index.php file into the sub-folders for each sub-domain. Once you have those copies in place, you can use distinct global variables easily.
    I’ve used the strategy outlined by Made by Hippo.
    For example, you can then set each sub-domain’s site_url variable:

    --- <a href="http://www.domain.com">http://www.domain.com</a> ---
    $assign_to_config['site_url'] = 'http://www.domain.com/';
    
    --- secondary domain/index.php ---
    $assign_to_config['site_url'] = 'http://sub.domain.com/';
    etc…

    Allong with those config items, you can assign sub-domain specific global variables like so:

    --- <a href="http://www.domain.com">http://www.domain.com</a> ---
    $assign_to_config['global_vars'] = array(
    "en"=>"English",
    "my-other-variable" => "my-other-value"
    ); // This array must be associative
    --- secondary domain/index.php ---
    $assign_to_config['global_vars'] = array(
    "fr"=>"French"
    "my-other-french-variable" => "my-other-french-value"
    ); // This array must be associative

    Let me know if you have any other questions.

    Cheers,

  • #7 / Mar 04, 2012 5:23pm

    fitzage

    100 posts

    The subdomains are actually all going to the same site, so they’ll all use the same index.php. So how would I do the logic in the index.php to assign the global variable then?

    I’d basically need your second block of code, but with some sort of if/then.

  • #8 / Mar 05, 2012 11:35am

    fitzage

    100 posts

    So once I actually paid attention to how your code sets the custom global variables, and then found a bit about how to check the domain, I got this which appears to work the way I want.

    if($_SERVER['HTTP_HOST'] == 'fr.domain.com') {
            $assign_to_config['global_vars'] = array(
               'mylang' => 'fr'
           ); 
        } else {
            $assign_to_config['global_vars'] = array(
               'mylang' => ''
           ); 
        }

    Then I should just be able to and an elseif for any other languages I add. I thought about just saving the subdomain into the global variable no matter what, but that means I’d have to have more template logic to check for the correct subdomains. This way it only gets saved if it’s a language I’m looking for, so it doesn’t matter.

  • #9 / Mar 05, 2012 11:37am

    fitzage

    100 posts

    I may also insert an underscore in the value for the global variable, since that’s how my custom fields are named. So then I don’t have to have more if/else code in the template to insert the underscore, I can just put {{mylang}body} and if {mylang} is empty, I get {body}. If it’s not empty, I get {fr_body} or whatever.

  • #10 / Mar 05, 2012 7:40pm

    Sean C. Smith

    3818 posts

    fitzage,

    Glad to see that Dan put you on the right track here. Is there anything else I can assist you with?

    Sean

  • #11 / Mar 05, 2012 7:43pm

    fitzage

    100 posts

    No, that should do it. Thanks.

  • #12 / Mar 05, 2012 10:48pm

    Enviromed

    375 posts

    Is it possible for you to post the final coding that works/  It would be greatly appreciated by many of us.

  • #13 / Mar 05, 2012 10:49pm

    fitzage

    100 posts

    The bit of code I posted above is what works, in the index.php file.

    if($_SERVER['HTTP_HOST'] == 'fr.domain.com') {
            $assign_to_config['global_vars'] = array(
               'mylang' => 'fr'
           ); 
        } else {
            $assign_to_config['global_vars'] = array(
               'mylang' => ''
           ); 
        }
  • #14 / Mar 05, 2012 10:54pm

    Enviromed

    375 posts

    Thanks Fitz.  Much appreciated.

  • #15 / Mar 08, 2012 1:42pm

    Shane Eckert

    7174 posts

    Hey fitzage,

    Thanks so much for sharing!

    Have a great rest of the week.

    Cheers,

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases