Al_
Al_

Reputation: 2509

Laravel 4: Using views in a package

I've created a very basic app in Laravel 4, it's something I'll be reusing a lot in various projects so it made sense to convert it to a package before i got too far, but I'm struggling to make the changes to get it working, which I think is largely due to figuring out how to access the various objects that are normally available in an app, eg View::make

I had the following code working in an app:

class PageController extends BaseController {

public function showPage($id)
{
            //do stuff
            return View::make('page/showPage')
                 ->with('id', $id)
                 ->with('page', $page);
}

for the package I have the following:

use Illuminate\Routing\Controllers\Controller;
use Illuminate\Support\Facades\View;

class PageController extends Controller {

public function showPage($id)
{
      //do stuff        
      return View::make('page/showPage')
                 ->with('id', $id)
                 ->with('page', $page);
}

However this does not load the blade template which is located at:

workbench/packagenamespace/package/src/views/page/showPage.blade.php

nor does this work:

return View::make('packagenamespace/package/src/page/showPage')

Also, I am wondering if what i have done with the use statements where I use the facade object correct, to me it seems like there should be a neater way to access things like the View object?

Upvotes: 6

Views: 6333

Answers (2)

Pri Nce
Pri Nce

Reputation: 711

// Serviceprovider.php

$this->loadViewsFrom(__DIR__.'/resources/views', 'laratour');

// Controller    

<?php
    
    namespace Mprince\Laratour\Controllers;
    
    use Illuminate\Http\Request;
    use Illuminate\Routing\Controller;
    
    class LaratourController extends Controller
    {
    
        public function index()
        {
            return view('laratour::index');
            // return view('packageName::bladeFile');
        }
        //END
    }

Upvotes: 0

bstrahija
bstrahija

Reputation: 518

You should read the docs: http://four.laravel.com/docs/packages

Specifically the part explaining loading views from packages ;)

return View::make('package::view.name');

If you don' want to use:

use Illuminate\Support\Facades\View;

Just do:

use View;

Or even without the use statement:

\View::make('package::view.name');

Upvotes: 16

Related Questions