laravel 如何在laravel中设置视图文件路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27458439/
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 set view file path in laravel?
提问by xdim222
I have a directory structure for laravel app like this:
我有一个 laravel 应用程序的目录结构,如下所示:
app/
admin/
controllers/
views/ -> for admin views
...
views/ -> for frontend views
How can I set the view path for controllers in admin? I don't want to use View::addLocation
or View::addNamespace
because I might have the same view file name for admin and frontend, and don't want to add a namespace for every View::make('namespace::view.file')
.
如何在管理员中设置控制器的视图路径?我不想使用,View::addLocation
或者View::addNamespace
因为我可能对 admin 和 frontend 具有相同的视图文件名,并且不想为每个View::make('namespace::view.file')
.
I see in http://laravel.com/api/4.2/Illuminate/View/View.htmlthere is a setPath
method, but how do I call it? View::setPath
raised undefined method error.
我在http://laravel.com/api/4.2/Illuminate/View/View.html看到有一个setPath
方法,但我如何调用它?View::setPath
引发未定义的方法错误。
回答by lukasgeiter
You have two ways to accomplish your goal. First, let's have a look at app/config/view.php
. That's where the path(s) for view loading are defined.
你有两种方法来实现你的目标。首先,让我们来看看app/config/view.php
。这就是定义视图加载路径的地方。
This is the default:
这是默认值:
'paths' => array(__DIR__.'/../views'),
Method 1: Load both directories
方法一:加载两个目录
You can easily add the admin directory to the array
您可以轻松地将管理目录添加到阵列
'paths' => array(
__DIR__.'/../views',
__DIR__.'/../admin/views
),
Now the big disadvantage of this: view names have to be unique. Otherwise the view in the path specified first will be taken.
Since you don't want to use a view namespace I suppose you don't want a syntax like admin.viewname
either. You'll probably like method 2 more ;)
现在最大的缺点是:视图名称必须是唯一的。否则将采用首先指定的路径中的视图。
由于您不想使用视图命名空间,我想您也不想要类似的语法admin.viewname
。你可能会更喜欢方法 2 ;)
Method 2: Change the view page at runtime
方法二:运行时更改视图页面
Every Laravel config can be changed at runtime using the Config::set
method.
每个 Laravel 配置都可以在运行时使用该Config::set
方法进行更改。
Config::set('view.paths', array(__DIR__.'/../admin/views'));
Config::set('view.paths', array(__DIR__.'/../admin/views'));
Apparently setting the config won't change anything because it is loaded when the application bootstraps and ignored afterwards.
显然,设置配置不会改变任何东西,因为它在应用程序引导时被加载并在之后被忽略。
To change the path at runtime you have to create a new instance of the FileViewFinder
.
Here's how that looks like:
要在运行时更改路径,您必须创建FileViewFinder
.
这是它的样子:
$finder = new \Illuminate\View\FileViewFinder(app()['files'], array(app_path().'/admin/views'));
View::setFinder($finder);
Method 3: Use addLocation but without default path
方法 3:使用 addLocation 但没有默认路径
You could also remove the default path in app/config/view.php
您还可以删除默认路径 app/config/view.php
'paths' => array(),
And then use View::addLocation
in any case (frontend and admin)
然后View::addLocation
在任何情况下使用(前端和管理员)
View::addLocation(app_path().'/views');
View::addLocation(app_path().'/admin/views');
回答by Purvesh
In the latest version 6 i am doing it this ways:
在最新的版本 6 中,我是这样做的:
View::getFinder()
->setPaths([
base_path('themes/frontend/views'),
base_path('themes/admin/views')]
)
回答by cthiele
In Laravel 5.5, other solutions did not work. In boot method of a service provider
在 Laravel 5.5 中,其他解决方案不起作用。在服务提供者的引导方法中
View::getFinder()->prependLocation(
resource_path('views') . '/theme'
);