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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:33:59  来源:igfitidea点击:

How to load view from alternative directory in Laravel 4

phplaravellaravel-4namespaces

提问by devo

In my Laravel 4 application's root directory, I have a folder themes. Inside the themesfolder, I have defaultand azure. How can I access view from this themes/defaultfolder in a specific route.

在我的 Laravel 4 应用程序的根目录中,我有一个文件夹themes. 在themes文件夹内,我有defaultazure。如何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/defaultfolder. Please explain this.

我需要从localhost/laravel/app/themes/default文件夹加载视图。请解释一下。

采纳答案by devo

Here I am not accessing my project from publicfolder. Instead of this I am accessing from project root itself.

在这里,我不是从public文件夹访问我的项目。而不是我从项目根本身访问。

I have seen a forum discussion about Using alternative path for viewshere. But I am little confused about this.The discussed solution was,

我看过一个关于here的论坛讨论。但我对此有点困惑。讨论的解决方案是,Using alternative path for views

You'd add a locationlike,

你要加个location赞,

View::addLocation('/path/to/your/views');

Then add namespacefor 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.phpor page.blade.phpfile from project_directory/app/themes/defualtfolder.

这将从文件夹渲染page.phppage.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)(可在任何服务提供商处使用)。