Search code examples
phplaravellaravel-5.3laravel-bladelaravel-views

How to set views folder destination during runtime in laravel


I'm trying to build a saas application (saas here means software as a service) in laravel 5.3. I'm trying to implement the view folder destination by extending the ViewServiceProvider. 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

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')

and it should access the respective theme Navbar folder or sub-view. To implement this I'm extending the ViewServiceProvider and in config/app.php I'm just commenting out the ViewServiceProvider and added the WebViewServiceProvider which I made, it is not setting the destination, here is my code:

namespace Nitseditor\System\Providers;

use Illuminate\View\FileViewFinder;
use Illuminate\View\ViewServiceProvider;
use Nitseditor\System\Models\Domain;

class WebViewServiceProvider  extends ViewServiceProvider
{


    /**
     *  Register View Folder
     *
     * @return void
     */
    public function registerViewFinder()
    {

        $this->app->bind('view.finder', function ($app) {
            $paths = $app['config']['view.paths'];

            $http_req = php_sapi_name() == 'cli' ? 'noetic.com' : $this->app['request']->server('HTTP_HOST');
            $domainName = explode(":", $http_req)[0];
            $domain = Domain::where('domain_name', $domainName)->first();

            if($domain)
            {
                foreach ($domain->themes as $theme)
                {
                    $paths = 'Nitseditor\System\Resources\Views\Themes\\' . $theme->theme_name;
                }
            }

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

My config\app.php looks like:

//        Illuminate\View\ViewServiceProvider::class,
Nitseditor\System\Providers\WebViewServiceProvider::class,

while executing and checking through controller I can see the destination paths of views are same as default

class DomainController extends Controller {

    public function checkDomain()
    {
        $app = App::make('config');
        dd($app);
    }
}

Views directory check

Edit

well I tried executing without conditional statements it happens to be same.

public function registerViewFinder()
{

    $this->app->bind('view.finder', function ($app) {

        $paths = 'Nitseditor\System\Resources\Views\Themes\Themeone';

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

Help me out with this.


Solution

  • Well I kept on trying to die dump the code and tried checking $app array. I guess when I try doing App::make::('config') laravel collects information from the configuration files and shows the configuration array of app in which dynamic configuration are not being displayed.

    Then I simply tried checking my views:

    return view('template');
    

    It displayed the blade file/views exactly what I required, well it might help someone. Don't panic with

    $app = App::make('config');
    dd($app);
    

    Cheers!!