Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 399
Al_ Avatar asked Feb 03 '13 22:02

Al_


People also ask

How do I view views in Laravel?

Views may also be returned using the View facade: use Illuminate\Support\Facades\View; return View::make('greeting', ['name' => 'James']); As you can see, the first argument passed to the view helper corresponds to the name of the view file in the resources/views directory.

How do I load a view in Laravel controller?

Loading a view in the controller is easy. You just need to add `view('viewname')` method while returning from the controller method. `view()` is a global helper in Laravel. In this method, you just need to pass the name of the view.

What is @yield used for in Laravel?

In Laravel, @yield is principally used to define a section in a layout and is constantly used to get content from a child page unto a master page.

What is the use of view in Laravel?

What are the views? Views contain the html code required by your application, and it is a method in Laravel that separates the controller logic and domain logic from the presentation logic. Views are located in the resources folder, and its path is resources/views.


1 Answers

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');
like image 58
bstrahija Avatar answered Oct 31 '22 16:10

bstrahija