Say I have theese routes:
http://mylaravelapp.com/{slug}
http://mylaravelapp.com/{slug}/page
http://mylaravelapp.com/{slug}/another/{custom_slug}
And I would like the same routes to be accessed from a custom domain, pointing to my laravel app's IP. Like this:
http://customdomain.com/
http://customdomain.com/page
http://customdomain.com/another/{custom_slug}
How is this achieved the best way?
My current, ugly way
I have made my own solution, which is rather ugly. It involves a lot of repeat-code, and a nasty controller. This is how I achieve it:
routes.php
/**
* Serving from my app:
*/
Route::get('/{slug}', ['uses' => 'MyController@show', 'as' => 'page']);
Route::get('/{slug}/page', ['uses' => 'MyController@page', 'as' => 'page.page']);
Route::get('/{slug}/another/{custom_slug}', ['uses' => 'MyController@another', 'as' => 'page.another']);
/**
* Serving from custom domain
*/
Route::group(['domain' => '{custom_domain}.{tld}'], function($domain) {
Route::get('/', ['uses' => 'MyController@show', 'as' => 'page.customdomain']);
Route::get('/page', ['uses' => 'MyController@page']);
Route::get('/another/{custom_slug}', ['uses' => 'MyController@another']);
});
MyController.php
class MyController extends Controller {
/**
* Find by slug or custom domain
*
* @return [type] [description]
*/
public function findBySlugOrDomain($domain, $domain_tld, $slug) {
if($domain && $domain_tld) {
/**
* Find by custom domain name
*/
$page = $this->page->findByDomain($domain.'.'.$domain_tld)->firstOrFail();
} else {
/**
* Find by slug (no custom domain)
* @var [type]
*/
$page = $this->page->findBySlugOrFail($slug);
}
return $page;
}
/**
* Display the specified resource.
*/
public function show($domain = null, $domain_tld = null, $slug = null, $type = 'home', $custom_slug = null)
{
/**
* Cases
*/
if(str_contains(config('app.url'), $domain . '.' . $domain_tld)) {
/**
* Incoming request to HOME (i.e. http://mylaravelapp.com/)
*/
return app('App\Http\Controllers\HomeController')->index();
} elseif($domain && !$domain_tld) {
/**
* Request to page on http://mylaravelapp.com/{slug}/page
*/
$slug = $domain;
$domain = null;
$domain_tld = null;
} else if($domain && $domain_tld && !$slug) {
/**
* Request to page with slug on http://mylaravelapp.com/{slug}/another/{custom_slug}
*/
$slug = $domain;
$custom_slug = $domain_tld;
$domain = null;
$domain_tld = null;
} else if($domain && $domain_tld && $slug) {
/**
* Request to page on http://customdomain.com/
*/
} else if($domain && $domain_tld && $slug) {
/**
* Request to page with slug on http://customdomain.com/another/{custom_slug}
*/
$custom_slug = $slug;
}
$page = $this->findBySlugOrDomain($domain, $domain_tld, $slug);
switch ($type) {
case 'page':
return view('page.page', compact('page'));
break;
case 'another':
$anotherPage = $page->another()->whereSlug($custom_slug)->firstOrFail();
return view('page.another', compact('page', 'anotherPage'));
break;
}
return view('page.home', compact('page', 'js_variables'));
}
/**
* Page: page
*
* http://mylaravelapp.com/{slug}/page
*
* http://customdomain.com/page
*/
public function showPage($domain = null, $domain_tld = null, $slug = null) {
return $this->show($domain, $domain_tld, $slug, 'gallery');
}
/**
* Page: another
*
* http://mylaravelapp.com/{slug}/another/{custom_slug}
*
* http://customdomain.com/another/{custom_slug}
*/
public function showAnother($domain = null, $domain_tld = null, $slug = null, $custom_slug = null) {
return $this->show($domain, $domain_tld, $slug, 'page', $custom_slug);
}
}
Limitations of this way:
- A lot of repeat code
- Every time I add a new route, I need to update it twice
- Long and in-understandable Controller
- A lot of new complexity for the controller, in case we need, say two custom slugs in the URL (
http://mylaravelapp.com/{slug}/another/{custom_slug}/{third_slug}
)