Php Laravel路由-相同路由的自定义域
说我有这些路线:Php Laravel路由-相同路由的自定义域,php,laravel,laravel-5,laravel-routing,Php,Laravel,Laravel 5,Laravel Routing,说我有这些路线: http://mylaravelapp.com/{slug} http://mylaravelapp.com/{slug}/page http://mylaravelapp.com/{slug}/another/{custom_slug} 我希望从自定义域访问相同的路由,指向我的laravel应用程序的IP。像这样: http://customdomain.com/ http://customdomain.com/page http://customdomain.com/an
http://mylaravelapp.com/{slug}
http://mylaravelapp.com/{slug}/page
http://mylaravelapp.com/{slug}/another/{custom_slug}
我希望从自定义域访问相同的路由,指向我的laravel应用程序的IP。像这样:
http://customdomain.com/
http://customdomain.com/page
http://customdomain.com/another/{custom_slug}
如何以最佳方式实现这一目标
我现在的丑陋方式
我已经制定了自己的解决方案,这相当难看。它涉及大量重复代码和一个讨厌的控制器。我就是这样做到的:
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']);
});
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);
}
}
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);
}
}
这种方式的局限性:
- 大量重复代码
- 每次添加新路线时,我都需要更新两次
- 长而易理解的控制器
- 如果我们需要的话,控制器会有很多新的复杂性,比如URL中的两个自定义段塞(
)http://mylaravelapp.com/{slug}/另一个/{custom_slug}/{third_slug}
server {
#implemented by default, change if you need different ip or port
#listen *:80 | *:8000;
server_name customdomain.com;
return 301 $scheme://mylaravelapp.com$request_uri;
}
这将改变:
http://customdomain.com/
http://customdomain.com/page
http://customdomain.com/another/{custom_slug}
为此,请自动执行以下操作:
http://mylaravelapp.com/
http://mylaravelapp.com/page
http://mylaravelapp.com/another/{custom_slug}
另一种方法。只是一个愚蠢的问题,你看过路由了吗?为什么不尝试将页面路由置于第一条{slug}路由之上?你能详细说明这个问题吗?对不起,忘了我的问题。我想你的路由的位置(顺序)有问题。为什么不需要路由中的{slug}?因为在我的域中,{slug}引用了特定的页面。访问自定义域时,无需将{slug}引用为“自定义域”,而是引用特定页面。