douhui1630
2016-09-16 15:17
浏览 44
已采纳

在Laravel中扩展刀片引擎以使用不同的视图目录

I am working on a Laravel project and I am very new to it. For now, I want to use blade templates to render views but I want it to search for views in different directories like <custom_dir>\views instead of default resources/views. The <custom_dir> will be dynamic (it can be a variable).

Any ideas? I was thinking of a custom service provider and then extend the default function which renders views in Laravel inside it. But not sure how to implement it.

Edit: I have user this link to extend the default functionality of include function in blade template engine. But this overrides the include functionality. I want to change the path and then call the default blade functionality

图片转代码服务由CSDN问答提供 功能建议

我正在开发一个Laravel项目,我对它很新。 目前,我想使用刀片模板来呈现视图,但我希望它能够在不同的目录中搜索视图,例如&lt; custom_dir&gt; \ views 而不是默认的 resources / views &lt; custom_dir&gt; 将是动态的(它可以是变量)。

任何想法? 我正在考虑一个自定义服务提供程序,然后扩展默认函数,在其中呈现Laravel中的视图。 但不确定如何实现它。

编辑: 我有这个用户 link 以扩展刀片模板引擎中include函数的默认功能。 但这会覆盖包含功能。 我想更改路径,然后调用默认刀片功能

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqjcb132285 2016-09-16 15:40
    已采纳

    You could probably append the path to the configuration:

    1) Statically, by modifying file config/view.php

    'paths' => [
        realpath(base_path('resources/views')),
        //more paths here
    ],
    

    2) Dynamically at runtime:

    $paths = config('view.paths');
    $paths[] = $newPathToAdd;
    config(["view.paths" => $paths ]);
    

    I suggest you use this in moderation otherwise you will just end up with a mess of directories with no real specified purpose.

    打赏 评论
  • duan1933 2016-09-16 15:19

    You can create custom directories in resources\views directory and use them with something like this:

    return view($customDirectory.'.index');
    

    Where index is a template inside custom directory.

    打赏 评论