Laravel 4, view composers against #include - laravel

I noticed if I pass a second parameter to #include like this:
#include('sidebars.pages', array('categories' => Category::all()))
Then it is possible to replicate the concept of render partials within views and render partials within partials like in Rails.
Do I still need view composers with this functionality?
I appreciate any help!

Try View Composers to bind data to views. Works best for partial views
// View Composer Example
View::composer(array('sidebars.pages'), function($view)
{
$view->with('categories', Categories::all());
});
#include('sidebars.pages')

While that may be possible it's not the documented use of #include. I'd use caution when doing it like that, and personally, I wouldn't be calling a model within your view. Bind the data you require from your route or controller. Make use of a presenter to perform any presentation logic to keep your views absolutely clean.
#include injects all currently defined variables into the nested partial view. So if you bound all the categories to the parent view, then used #include('sidebars.pages'), that view would also have the categories bound to it.

Related

How can I use 2 models in a single view?

I'm currently working on a homepage where I am showing the 'latest hauls' and 'latest finds' they are both separate models ofcourse.
Currently I am only showing the 5 latest hauls since I use the HaulController#getWelcome Controller so I can access $haul->title etc.
How would I be able to also access $finds->title?
Thanks for the help!
You can share a model to a view by using it inside controller function -
View::share('subadmin', App\Models\SubAdmin::class);
You can call the model in your view like
{{ \App\Model::function() }}
So you can have {{ App\Model::orderBy('created_at', 'desc')->limit(5)->get() }}
You have a couple of options.
Option 1: A combined model and one view.
To do this create a new ViewModel and have LatestHauls and LatestFinds as properties on this model. You could set these properties in the controller.
On your view simply navigate down the viewmodel to the appropriate properties.
Option 2: Extend the above solution to use partial views. Create a partial view for each model. On the parent view call each partial view. This might be considered a better solution but option 1 will get you started.

How to make a laravel 5 view composer

I'm still learning Laravel and I'm working on a small project to help me understand better. In the project, I am in need of a global array, so that I may display it or its attributes on every view rendered. sort of on a notification bar, so that each page the user visits, he/she can see the number of notifications (which have been fetched in the background and are stored in the array).
I have done some research, and realized that I have to fetch and compile the array in a view composer I think. But everywhere I go, I cant seem to understand how to make a view composer.
I need to fetch the relevant rows from the database table, and make the resulting array available to each view rendered (I'm thinking attaching it somehow to my layouts/default.blade.php file.). Please help, any and all advice is greatly appreciated:)
You can now inject services on your view
More info here: https://laracasts.com/series/whats-new-in-laravel-5-1/episodes/2
You have to use Sub-Views of laravel blade. I guess your functionality is like a sidebar or like a top bar which will be rendered at every page.
//Your Controller pass data
class YOUR_CONTROLLER extends Controller {
public function index()
{
$data = YOUR_DATA;
return view('YOUR_VIEW_FILE', get_defined_vars());
}
}
//In Your View File
#extends('LAYOUTS_FILE')
#section('YOUR_SECTION')
#include('YOUR_SUB_VIEW_FOR_NOTIFICATION')//You need not pass any data passed all data will be available to this sub view.
#endsection
In your sub view
//Do what ever you want looping logic rendering HTML etc.
//In your layout file just yield or render the section that's it
#yield('YOUR_SECTION')
More explanation can be found Including Sub-Views

Can I declare a view composer for a Form or HTML macro in Laravel 4?

I have a partial view that I am using to provide a pair of form select fields, and both sets of options depend on some variables set by a view composer for this view.
I would like to implement this view as a Form::macro instead so that I can parameterize some parts of it. However, I'm not sure how I can point the view composer at a macro instead of a partial view. Is this even possible?
I could go the route of pointing the composer at any view that I'm using the macro in, but I'd much rather have the option data load automatically any time I use the macro as it is a common component in my web app.
I use partials for some Bootstrap UI components, and create a macro to connect data into the template, so I only use HTML::macro or Form::macro to access them:
Form::macro('myMacro', function($data) {
return View::make('myPartial', $data);
};
This doesn't really answer the primary question, but one way I can get what I want is to use View::Make instead of #include in my views.
Where I was using:
#include('thepartial')
I can use something like this instead:
{{ View::make('thepartial')->with(array('param1'=>$param1, 'param2'=>$param2, ...) }}
This way I get the parameterization I want, and I still get the composer's effects.

Laravel 4 passing data to #include

I have a view called admin.users. In that view I include header and footer using #include directive. I write all my views-including stuff in routes.php, so for admin.users it is:
Route::get('users', function() {
// ...
return View::make('admin.users')->with('num', $usersNum);
}
And in users.blade.php:
#include('admin.partials.header')
// ....
#include('admin.partials.footer')
Is it possible to pass "users num" to header view in order to show that variable? And is it a good practice the way I'm combining views, because I read about controller layouts but actually I decided to have only rest controllers while I include view only in routes.php (like load static markup and after that communicate with server by ajax)
Have a look at 'ViewComposers' (http://laravel.com/docs/responses#view-composers). This is a great way to share data with your views and keep your routes file clean.
In global.php (or any other place really) add:
View::composer('admin.users', function($view)
{
// Do your $usersNum logic here
...
$view->with('num', $usersNum);
});
If at some point you want this data to be available in admin.dashboard as well, just rewrite to:
View::composer(array('admin.users', 'admin.dashboard'), function($view)
As I said in my comment (and what seems to have fixed your issue) you should create an admin.template view with your header and footer in it, and start your views with #extends('admin.template').

Ember.js: How can I decouple my views from my controllers?

I'm trying to restructure tightly coupled portions of an ember.js app, particularly views and templates to controllers.
All the examples I've seen bind views directly to controllers in the view's class definition, or by pass the (global) controller path to the view in the template itself.
The TargetActionSupport mixin (DelegateSupport in sproutcore, I think) seems like a good candidate, but would still require the target (controller) and action to be set in the template itself.
Ideally, I would like to instantiate my views in my controller (somehow), set targets and actions, but also set presentational variables in my templates (ex: static classes, id), but I'm not sure how to accomplish something this, or whether it's the right approach.
You can programmatically instantiate views and insert them into the DOM wherever you'd like:
var view = Ember.View.create();
view.appendTo('#someElement');
If you want to eliminate global binding paths, you can pass in a controller reference to the view on instantiation:
var controller = Ember.Object.create({content: {}}),
view = Ember.View.create({
controller: controller,
contentBinding: 'controller.content'
});
I'd also suggest looking into the Ember StateManager.

Resources