Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nitishnoetic's avatar

Dynamic view folder destination in laravel 5.3

I'm making an application on Laravel 5.3. I'm trying to set dynamic paths of views for my theme based application. I tried almost everything but don't know why it is not changing/adding. This is continuation of following question:

https://laracasts.com/discuss/channels/code-review/how-to-set-views-folder-destination-during-runtime-in-laravel

Well I also tried making a webViewServiceProvider as ServiceProvider and trying to set the views in the boot method:

$view_path = base_path('Nitseditor\System\Resources\Views\Themes\\'.$themename);

View::addLocation($view_path);

but it is not adding to the path. while making a Config::set() I'm able to change the path but while executing it is not finding the blade file.

Config::set('view.paths', array($view_path));

Even this doesn't worked for me:

$finder = new \Illuminate\View\FileViewFinder(app()['files'], array(app_path().$themename));
View::setFinder($finder);

And even this:

App::make('view.finder')->addLocation( 'my_views_path' );

I don't know where I'm making mistake, been stuck for last couple of days. Help me out.

0 likes
7 replies
sid405's avatar

is "Nitseditor\System\Resources\Views\Themes\" actually a path or is this a namespace of sorts?

nitishnoetic's avatar
nitishnoetic
OP
Best Answer
Level 3

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!!

martinbean's avatar

@nitishnoetic I’m making a SaaS CMS that uses themes. The way I approached it was to use namespaced views, and every “site” has a service provider that prepends a path to the namespace hints.

So in my AppServiceProvider.php file:

public function boot()
{
    $this->app['view']->addNamespace('cms', config('view.paths'));
}

And then in each site’s service provider class:

public function boot()
{
    // IMPORTANT
    // You should determine whether the theme should be loaded or not here
    // This could be based on the hostname or something else
    // Otherwise each theme would override the last if you include them all in config/app.php
    $themeShouldLoad = true;

    if ($themeShouldLoad) {
        $this->app['view']->prependNamespace('cms', __DIR__.'/../resources/views');
    }
}

This means I can have a base “theme” in my core application, and then each theme can override any views it needs to (i.e. if I need to modify the HTML for that particular theme only). But the idea for me is to keep theme overrides to a minimum for maintainability.

This does come with the caveat that you need to include the namespace in all of your views:

@extends('cms::layouts.app')

Including in controller actions:

class HomeController extends Controller
{
    public function __invoke()
    {
        return view('cms::home');
    }
}
1 like
nitishnoetic's avatar

@martinbean it's a very good idea of implementing the views into namedspace. But I wanted to have without namedspace, though I know it will overwrite the views if present in main resources/views folder. But my resources/views folder is kind of empty.

Thanks for the update!

1 like
martinbean's avatar

@nitishnoetic I agree, it would be nice to not have to have them namespaced, but at the time it was the only way I could get views set up the way I wanted them, as the prependLocation()> method on the view finder isn’t public; I could only append which isn’t what I wanted.

sean_connell_93's avatar

I had a use case where I needed to display a different view depending on the incoming request. I have 2 base folders inside the resources/views directory, one it the default and the other set based on a condition.

if (true) {
$baseViewPath = 'resources/views/custom/';
view()->getFinder()->setPaths([$baseViewPath]);
}

I could have also used one of these

view()->getFinder()->addLocation($baseViewPath);
// or place it first in the list
view()->getFinder()->prependLocation($baseViewPath);

Please or to participate in this conversation.