在laravel 5.3中的运行时具有动态View文件夹路径 [英] Having a dynamic View folder path during runtime in laravel 5.3

查看:50
本文介绍了在laravel 5.3中的运行时具有动态View文件夹路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 laravel 5.3 中构建一个saas应用程序(这里的saas表示软件即服务).我建立了一些收集域名,正在使用的主题和那些特定网站的数据库的服务提供商.现在,我正在尝试通过 service provider view结构来实现页面.现在,例如,对于两个不同的领域,我有两个不同的主题.我在不同文件夹的视图中有一组HTML代码,如下所示:

I'm trying to build a saas application (saas here means software as a service) in laravel 5.3. I've build few service provider which collects the domain name, the theme being used and databases of those particular website. Now I'm trying to implement the pages with view structure through service provider. Now for example I'm having two different themes for two different domains. I'm having the set of HTML code in views in different folders, something like this:

View
----| Theme One
--------| Navbar
--------| Sliders
--------| Tabs
--------| Parallax
--------| Iconbox
--------| template.blade.php
----| Theme Two
--------| Navbar
--------| Sliders
--------| Tabs
--------| Parallax
--------| Iconbox
--------| template.blade.php

现在,我想为这些域动态定义文件夹结构,以便它应显示其各自主题的模块.就像假设如果要包含Navbar的子视图,我只需要写

Now I want to define folder structure dynamically for these domains so that it should show the modules of their respective theme. Like suppose if I want to include sub-view of Navbar I just have to write

@include('Navbar')

,它应该访问相应的主题Navbar文件夹或子视图.我想到了做一个服务提供商,并试图通过config设置如下路径:

and it should access the respective theme Navbar folder or sub-view. I thought of making a service provider and trying to set via config the path something like this:

public function boot()
{
    $this->webView = $this->setPath();
    $this->app->singleton('webView', function()
    {
        return $this->webView;
    });
}

public function setPath()
{
    $themename = App::make('themename')
    if($themename)
    {
        $setpath = "..Path\Views\" . $themename;
        Config::set('view.paths', $setpath);
        return null;
    }
    else
    {
        return "Not found";
    }
}

但是我想当应用程序启动时它将忽略配置,我知道必须有更好的方法来实现这一点.请引导我.

But I guess when the application bootstraps it will ignore the configuration, I know there must be a better way in implementing this. Please guide me.

推荐答案

首先在 App \ Providers ,或在 app/Providers &中复制 Illuminate \ View \ ViewServiceProvider 像这样改变

First create a ViewServiceProvider in App\Providers like this or copy Illuminate\View\ViewServiceProvider in app/Providers & change like this

<?php

namespace App\Providers;

use Illuminate\View\FileViewFinder;
use Illuminate\View\ViewServiceProvider as ConcreteViewServiceProvider;

class ViewServiceProvider extends ConcreteViewServiceProvider
{
    /**
     * Register the view finder implementation.
     *
     * @return void
     */
    public function registerViewFinder()
    {
        $this->app->bind('view.finder', function ($app) {
            $paths = $app['config']['view.paths'];

            //change your paths here
            foreach ($paths as &$path)
            {
                $path .= time();//change with your requirement here I am adding time value with all path
            }

            return new FileViewFinder($app['files'], $paths);
        });
    }
}

然后将 Illuminate \ View \ ViewServiceProvider :: class, 替换为 App \ Providers \ ViewServiceProvider :: class, 中的 config/app.php .这可以解决问题.

then replace your Illuminate\View\ViewServiceProvider::class, with App\Providers\ViewServiceProvider::class, in config/app.php. This will do the trick.

这篇关于在laravel 5.3中的运行时具有动态View文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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