我想在laravel 5.1中设置多个域。
我有3个不同的域名
www.domain1.com
www.domain2.com
www.domain3.com
每个域都有一些共同的功能和一些不同的功能,但具有不同的主题
我想要我的视图结构
resource/www.domain1.com
resource/www.domain2.com
resource/www.domain3.com
我无法分开观点如何实现这一目标?
**
答案 0 :(得分:3)
您应该创建中间件来修改查看文件夹的路径:
namespace App\Http\Middleware;
use Closure;
use Illuminate\View\FileViewFinder;
use Illuminate\Support\Facades\View;
class ModifyViewFolder
{
public function handle($request, Closure $next)
{
$finder = new FileViewFinder(app()['files'], [
app_path('../resources/views/' . $request->server->get('HTTP_HOST')),
app_path('../resources/views/'),
]);
View::setFinder($finder);
return $next($request);
}
}
然后在您的Kernel.php
中注册。如果域特定文件夹中的视图不存在,这将回退到默认文件夹。
答案 1 :(得分:0)
我理解这篇文章相当古老,但与Laravel的所有内容一样,有多种方法可以做任何事情。我可以通过覆盖ViewServiceProvider来实现这种效果。但是,此解决方案将适用于整个站点,因为已接受的答案将允许您将模板应用于Web组。
类
TemplateServiceProvider extends ViewServiceProvider
{
/**
* Register the view finder implementation.
*
* @return void
*/
public function registerViewFinder()
{
$this->app->bind('view.finder', function ($app) {
$paths = [];
/*
* Pull our template from our site name
*/
$template = Template::where('domain', Request::server('SERVER_NAME'))->first();
if($template)
{
$paths = [
$app['config']['view.paths.templates'] . DIRECTORY_SEPARATOR . $template->tag
];
}
/*
* Default view path is ALWAYS last
*/
$paths[] = $app['config']['view.paths.default'];
return new FileViewFinder($app['files'], $paths);
});
}
}