Reputation: 81
I want to detect subdomains in routes.php in my L4 website and want to store that subdomain value somewhere so that I can access that value in each controller.
How can I do that ? Please help
Upvotes: 2
Views: 5739
Reputation: 1291
This is working on my Laravel 4.2 right now.
On your routes file:
Route::group(['domain' => '{subdomain}.myapp.com'], function()
{
Route::get('/', function($subdomain)
{
return "Your subdomain is: ".$subdomain;
});
});
Get subdomain in your controllers or anywhere:
$subdomain = Route::getCurrentRoute()->getParameter('subdomain');
Upvotes: 0
Reputation: 87719
You can use Request to get your domain anywhere.
So, create a BaseController and add a method to get the current domain on all your extended controllers:
class BaseController extends Controller {
public function getDomain()
{
return Request::getHost();
}
}
And use it:
class PostsController extends BaseController {
public function store()
{
$post = new Post;
$post->domain_id = Domain::where('name', $this->getDomain())->first()->id;
$post->save();
}
}
Of course, this controller example supposes that you have a Domain model:
class Domain extends Eloquent {
private $table = 'domains';
}
EDIT:
Unless you have a very good reason for it, you don't have to use routes or store your subdomain on a session, unless you have a really good reason for this, it's a smell. Take a look at Laravel's code, there only one session stored by it: Laravel's session.
You can create a helper function:
Create a app/helpers/functions.php file (this is just an example) and add this helper function there:
function getCurrentSubdomain()
{
$domain = Config::get('app.domain');
preg_match("/^(.*)(\.$domain)$/", Request::getHost(), $parts);
return $parts[1];
}
Open your app/config/app.php and add a domain configuration to it:
return array(
'domain' => 'myapp.com',
...
);
Add the file to the autoload section of your composer.json:
"autoload": {
"classmap": [
...
],
"files": [
"app/helpers/functions.php"
]
},
Then you can use it everywhere: controllers, classes, routers, etc. Here's the same example as before, using it:
class PostsController extends BaseController {
public function store()
{
$post = new Post;
$post->domain_id = Domain::where('name', getCurrentSubdomain())->first()->id;
$post->save();
}
}
You can also create a class and a Facade for it, so you can call this class from anywhere, like Laravel does:
Helper::getCurrentSubdomain();
Or you can do the same by just creating a class and create a static function (less testable).
Upvotes: 4
Reputation: 931
You can put this type of method in routes. However, I think it's a better fit for a filter in the 'app/filters.php' file. Try this:
Route::filter('getSubdomain', function($route, $request)
{
$host = $request->getHost();
$parts = explode('.', $host);
$subdomain = $parts[0];
// Store subdomain in session
Session::put('subdomain', $subdomain);
});
Then add the filter to the route (probably a group route) as follows:
Route::group(array('before' => 'getSubdomain'), function()
{
... add route stuff here ..
});
You can read more about how to use Laravel filters here:
http://laravel.com/docs/routing#route-filters
Upvotes: 8
Reputation: 180004
http://laravel.com/docs/routing#sub-domain-routing
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('user/{id}', function($account, $id) {
//
});
});
Upvotes: 11