php 如何从 Laravel 4 中的替代目录加载视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18954758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load view from alternative directory in Laravel 4
提问by devo
In my Laravel 4 application's root directory, I have a folder themes
. Inside the themes
folder, I have default
and azure
.
How can I access view from this themes/default
folder in a specific route.
在我的 Laravel 4 应用程序的根目录中,我有一个文件夹themes
. 在themes
文件夹内,我有default
和azure
。如何themes/default
在特定路径中从此文件夹访问视图。
Route::get('{slug}', function($slug) {
// make view from themes/default here
});
My directory structure:
我的目录结构:
-app
-应用程序
--themes
--主题
---default
- -默认
---azure
---天蓝色
I need to load views from localhost/laravel/app/themes/default
folder. Please explain this.
我需要从localhost/laravel/app/themes/default
文件夹加载视图。请解释一下。
采纳答案by devo
Here I am not accessing my project from public
folder. Instead of this I am accessing from project root itself.
在这里,我不是从public
文件夹访问我的项目。而不是我从项目根本身访问。
I have seen a forum discussion about Using alternative path for views
here. But I am little confused about this.The discussed solution was,
我看过一个关于here的论坛讨论。但我对此有点困惑。讨论的解决方案是,Using alternative path for views
You'd add a location
like,
你要加个location
赞,
View::addLocation('/path/to/your/views');
Then add namespace
for theme,
然后添加namespace
主题,
View::addNamespace('theme', '/path/to/themes/views');
Then render it,
然后渲染,
return View::make('theme::view.name');
What will be the value for /path/to/
?
将有什么价值/path/to/
?
Can I use the same project in different operating system without changing the path?
我可以在不同的操作系统中使用相同的项目而不改变路径吗?
Yes, we can do this using the following,
是的,我们可以使用以下方法来做到这一点,
Put the following in app/start/global.php
将以下内容放入 app/start/global.php
View::addLocation(app('path').'/themes/default');
View::addNamespace('theme', app('path').'/themes/default');
Then call view like the default way,
然后像默认方式一样调用视图,
return View::make('page');
This will render page.php
or page.blade.php
file from project_directory/app/themes/defualt
folder.
这将从文件夹渲染page.php
或page.blade.php
文件project_directory/app/themes/defualt
。
回答by Chirag Shah
This is entirely possible with Laravel 4. What you're after is actually the view environment. You can register namespace hints or just extra locations that the finder will cascade too. Take a look here
这在 Laravel 4 中是完全可能的。你所追求的实际上是视图环境。您可以注册命名空间提示或只是查找器也将级联的额外位置。看看这里
You'd add a location like so:
您可以像这样添加一个位置:
View::addLocation('/path/to/your/views');
It might be easier if you namespace them though, just in case you have conflicting file names as your path is appended to the array so it will only cascade so far until it finds an appropriate match. Namespaced views are loaded with the double colon syntax.
但是,如果您命名它们可能会更容易,以防万一您的文件名冲突,因为您的路径已附加到数组,因此它只会级联到目前为止,直到找到合适的匹配项。命名空间视图使用双冒号语法加载。
View::addNamespace('theme', '/path/to/themes/views');
return View::make('theme::view.name');
You can also give addNamespace an array of view paths instead of a single path.
您还可以为 addNamespace 提供一组视图路径而不是单个路径。
回答by igaster
I've developed a theme package for laravel 5 with features like:
我为 laravel 5 开发了一个主题包,具有以下功能:
- Views & Asset seperation in theme folders
- Theme inheritence: Extend any theme and create Theme hierarcies
- 主题文件夹中的视图和资产分离
- 主题继承:扩展任何主题并创建主题层次
Try it here: igaster/laravel-theme
在这里试试:igaster/laravel-theme
回答by Mahmoud Zalt
\View::addLocation($directory);
works fine but the new right way to do it is using loadViewsFrom($path, $namespace)
(available on any service provider).
\View::addLocation($directory);
工作正常,但新的正确方法是使用loadViewsFrom($path, $namespace)
(可在任何服务提供商处使用)。