Real Dreams
Real Dreams

Reputation: 18038

Access to a package views

I'm trying to use the views of my custom package without adding them to view.php conf file. I have created the following service provider and added it to app.php file.

class FooServiceProvider extends ServiceProvider
{
    public function boot()
    {
      $this->loadViewsFrom(__DIR__.'/../views', 'foo');
    }
    public function register()
    {
    }
}

I tried to use a package view by view('foo.test'). The view file is located in 'packages/foo/bar/views/test.blade.php'. However Laravel can not yet find the view file. Is there anything more I need to do? BTW, I do not need to publish view files to resource/views folder.

Upvotes: 0

Views: 889

Answers (1)

Nitish Kumar
Nitish Kumar

Reputation: 6296

Once you have loaded the views in boot as you are doing right now:

class FooServiceProvider extends ServiceProvider
{
    public function boot()
    {
      $this->loadViewsFrom(__DIR__.'/../views', 'foo');
    }
    public function register()
    {
    }
}

Check your service provider loads from the appropriate folder as right now you are having packages/foo/bar/views/teset.blade.php so your service provider needs to be in packages/foo/bar/providers it can be providers or any other folder name just quoted for example and please make sure about the spell check, you are having blade file named teset and you are calling test then finally you can call this view in controller with something like this:

return ('foo::test')

Update: Well as per the requirement you need to make changes in config on fly then this you need to have service provider something like this:

use Illuminate\View\FileViewFinder;
use Illuminate\View\ViewServiceProvider;

class WebViewServiceProvider  extends ViewServiceProvider
{

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

        $this->app->bind('view.finder', function ($app) {
            $paths = 'your view directory';

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

Hope this helps.

Upvotes: 1

Related Questions