从路径Laravel 5渲染视图 [英] Render view from path Laravel 5

查看:50
本文介绍了从路径Laravel 5渲染视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Laravel 5中构建一个模块化系统,并且系统的模块视图不在/resources/views 之外.它们在/modules/XXXXModule/core/views 下.

I'm building a modular system in Laravel 5 and the modules views of my system are outside from the /resources/views. They are under /modules/XXXXModule/core/views.

当我使用 return view('viewFileHere')时,这在逻辑上不起作用,因为它在错误的位置寻找视图.

When I use the return view('viewFileHere') it doesn't work (logically) because is looking for the views in the wrong place.

如何告诉Laravel 5在其他位置(路径)搜索视图?

How can I tell Laravel 5 to search for the views in other place (path)?

推荐答案

在Laravel中有两种为视图添加新路径的方法:

There are two ways of adding new paths for views in Laravel:

1.通过使用 addLocation :

1. By using addLocation:

view()->addLocation('/modules/XXXXModule/core/views');
view()->addLocation('/modules/YYYYModule/core/views');

上面的方法可以工作,但是如果您在每个文件夹中都有一个 list.blade.php 视图,请使用以下方法:

The above will work but if you have a list.blade.php view in each of the folders, if you use this:

return view('list');

它将始终返回在第一个注册的位置中找到的那个,在这种情况下,它将是用于 XXXXModule 的位置.因此,尽管它起作用了,但是对于您的模块化结构却有其缺点.

it will always return the one it finds in the location registered first, which in this case would be for XXXXModule. So while it works it has its drawbacks for your modular structure.

2.通过使用 addNamespace :

2. By using addNamespace:

view()->addNamespace('XXXXModule', '/modules/XXXXModule/core/views');
view()->addNamespace('YYYYModule', '/modules/YYYYModule/core/views');

上面的代码将每个路径注册到一个不同的命名空间中,因此,如果您有两个名称相同的视图文件(即 list.blade.php ),但它们属于两个不同模块的一部分,则可以访问它们如下:

The above will register each path in a different namespace, so if you have two view files with the same name (i.e. list.blade.php) but are part of two different modules you can access them as following:

// for XXXXModule
return view('XXXXModule::list');

// for YYYYModule
return view('YYYYModule::list');

这样,您就不再有位置冲突了.

This way you have no more conflicts of location.

这篇关于从路径Laravel 5渲染视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆