Question

I am pretty new to Laravel, so haven't worked out all the little tricks yet. I'm making little widgets that can be reused around my site (Calendar, Image cropper, map etc..), as blade templates and including them in other views like this:

@include('widgets.calendar', array('month'=>$month, 'year'=>$year))

My problem is I cannot find a way to include it in a view, where it still uses it's own controller to bring in some data before displaying it. Only the controller of the page it sits on is running.

I have a route set up which connects the controller ok when browsing directly to the widget's view:

Route::get('/widgets/calendar', array('as' => 'widgets.calendar', 'uses' => 'widgets\CalendarController@getCalendar'));

Does anyone know of a way I can achieve this, or am I going about it the wrong way?

Was it helpful?

Solution

In my opinion, if you do a new request for every one of the many widgets a page may have, your users will suffer in experience, because it may get slower and slower.

If your problem is the data you need on your widgets, you can use View::composers():

View::composer(array('widgets.calendar'), function($view)
{
    $view->with('month', new \Carbon\Carbon);
    $view->with('year', new \Carbon\Carbon);
});

And $month and $year will be available to those views, so you don't need to pass them in the include:

@include('widgets.calendar')

You can create a file for this purpose, something like app/composers.php and load it in your app/start/global.php:

require app_path().'/composers.php';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top