laravel load view from a folder name with actual dot

我只是一个虾纸丫 提交于 2019-12-13 18:56:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!