问题
I am creating a multi-domain Laravel app, so my view files are in separate folders per domain. For example, the following:
return view('pages/' . $_SERVER['SERVER_NAME'] . '/public/home', []);
should load a view under
pages/domain.com/public/home.blade.php
but instead it attempts to load
pages/domain/co/public/home.blade.php
because of the dot notation.
How do I get around this?
回答1:
You'd need to add a view namespace to set up hints for a particular folder if there're dots in the name.
$domain = 'domain.com';
View::addNamespace($domain, config('view.paths')[0]."/{$domain}/");
Route::get('example', function() use ($domain) {
return view("{$domain}::home");
});
You could use base_path('resources/views')
instead of in the example above config('view.paths')[0]
which is probably a bit more sensible in case someone reorders or changes the value of config('view.paths')
回答2:
Maybe you could replace the dot with underscore:
$domain = str_replace('.', '_', $_SERVER['SERVER_NAME']);
return view('pages/' . $domain . '/public/home');
and load the view under:
pages/domain_com/public/home.blade.php
回答3:
It should work
return view('pages/domain.com/public/home',[]);
来源:https://stackoverflow.com/questions/33721328/laravel-load-view-from-a-folder-name-with-actual-dot