4

I'm creating a plataform to support multiple websites under the plataform, and some websites needs to have a different domain.

The most commom is to use the same domain:

myrootdomain.com/first_product/profile

myrootdomain.com/second_product/profile

In these cases, the {first_product} and {second_product} will be passed as argument to almost all functions. /first_product and /second_product are total different websites running under the same plataform.

But I need to create another product where I can change the TLD and still be able to identify {anotherrootdomain} as the first parameter to my functions. Something like this:

anotherrootdomain.com/profile

I'm already handling the first parameter using Route::bind.

<?php

Route::bind('short_url', function($value, $route)
{
    $product = Product::where('short_url', $value)->first();

    if(is_null($product))
        return false;
    return $product->id;
});

Route::get('/{short_url}/login', 'HomeController@login');
Route::get('/{short_url}/profile', 'UserController@profile');

Now I don't know how to:

1) look for domain

2) use the domain as the first parameter

I know my english is terrible, but I hope you can help me!


EDIT:

I can do something like this on routes.php:

if($_SERVER['HTTP_HOST'] == "anotherrootdomain.com")
    $tld = "anotherrootdomain";

Then I need to chance from this:

Route::get('/{short_url}/login', 'HomeController@login');
Route::get('/{short_url}/profile', 'UserController@profile');

To this:

Route::get('/login', 'HomeController@login');
Route::get('/profile', 'UserController@profile');

And I can do it using a simple if, but now I can't pass the parameter on my routes! HomeController@login expects a parameter, and I need to pass $tld variable, but I don't know how!

tereško
  • 58,060
  • 25
  • 98
  • 150
  • Actually, your English is fine. – bjb568 Apr 03 '14 at 03:41
  • Thanks @bjb568, I make some mistakes but the most important is to deliver the message! :) – Wagner Mariotto Bonfiglio Apr 03 '14 at 06:28
  • Maybe you are looking for `sub-domain` solution ? – The Alpha Apr 03 '14 at 06:55
  • 1
    I think it's different, @SheikhHeera. I want a new Top Level Domain, not just a sub-domain. – Wagner Mariotto Bonfiglio Apr 03 '14 at 07:13
  • can all the top domains access the same laravel system folders? are those routes defined in the same route file? – itachi Apr 03 '14 at 09:43
  • I think you can indeed use Laravel's built in domain matching for what you want. Check out [the docs on sub-domain routing](http://laravel.com/docs/routing#sub-domain-routing) but just mentally replace any mention of subdomains with TLDs. I bet it'd still work the same, just as long as all the domains are [parked (in cPanel talk)](http://docs.cpanel.net/twiki/bin/view/AllDocumentation/CpanelDocs/Glossary#ParkedDomain) on the one hosting account. – alexrussell Apr 03 '14 at 10:35
  • Hi @itachi, all TLD will access the same laravel folders. It's like a premium feature: if you get the free account, your URL will look like mysystem.com/yoursystem, but if you pay you can get yoursystem.com to access the same system... – Wagner Mariotto Bonfiglio Apr 03 '14 at 19:16
  • @alexrussell, I'll try to do something with sub-domain routing, but the first thing I need to solve is: if the {short_url} on 'domain' => '{short_url}.com' is "myrootdomain", I need to add /{short_url}/ on the "Route::get" calls, and if '{short_url}' is a different domain I'll call Route::get("/login") and Route::get("/profile")... So, how can I access the {short_url} inside the Route::group? (It's confusing, I know) – Wagner Mariotto Bonfiglio Apr 03 '14 at 19:21

1 Answers1

4

I realized a way to do it!

Let's say that my /second_product on the example needs to be accesible using another TLD, like "awesomeproduct.com". Some URLs need to chance:

myrootdomain.com/second_product/login => awesomeproduct.com/login

myrootdomain.com/second_product/profile => awesomeproduct.com/profile

While the first_product still just working the first way:

myrootdomain.com/first_product/login

myrootdomain.com/first_product/profile

To make it possible, I created a route group with sub-domain (I didn't know that I could also use wildcard on TLD):

Route::group(array('domain' => '{short_url}.com'), function($short_url)

And I changed a little bit my Route::bind to change the {short_url} parameter:

Route::bind('short_url', function($value, $route)
{
    if($value == "awesomeproduct")
        $value = "second_product";

    $product = Product::where('short_url', $value)->first();

    if(is_null($product))
        return false;
    return $product->id;
});

Finally, I need to change my routes to use or not the {short_url}:

$tld = "";
if($_SERVER['HTTP_HOST'] != "testefacop.com")
    $tld = "{short_url}/";

Route::get('/'.$tld.'login', 'HomeController@login');
Route::get('/'.$tld.'profile', 'UserController@profile');

And here is my complete routes.php:

<?php

Route::bind('short_url', function($value, $route)
{
    if($value == "awesomeproduct")
        $value = "second_product";

    $product = Product::where('short_url', $value)->first();

    if(is_null($product))
        return false;
    return $product->id;
});

Route::group(array('domain' => '{short_url}.com'), function($short_url)
{
    $tld = "";
    if($_SERVER['HTTP_HOST'] != "awesomeproduct.com")
        $tld = "{short_url}/";

    Route::get('/'.$tld.'login', 'HomeController@login');
    Route::get('/'.$tld.'profile', 'UserController@profile');

}

It isn't a great and beatifull solution, but it's working as expected!

  • Glad you solved it :) This is pretty much what I was thinking with my comment (in fact you did me one better, my thought was to have two route groups for the two different situations). You should probably mark your answer as accepted if it solves the question btw. Self-promoting I know but that's just how it goes sometimes! – alexrussell Apr 04 '14 at 08:51
  • Thanks @alexrussell, your comment really opened my mind to find the solution! – Wagner Mariotto Bonfiglio Apr 18 '14 at 20:04