I am wondering if there are any way in laravel to send a variable to the layout master and keep it there while the user is logged in. I know how I can do it, like this
$this->layout->with('catalog.categories', $data);
But the problem of this is that I will have to be sending the variable on all the controllers and through all the class. And to get the variable I will need seven or eight lines of code on top of that.
I will give you an easy example. Imagine I have a messenger center in my web. And I want to show the user all the messages he did not read on the top bar. It has to be on the layout master and stay there until he read the messages. Do I have to pass this variable every single time the user changes the route?
Laravel has a nice way to handle issues where a same data is used in all views. View Composer is what you are looking for.
View::composer(array('view1','view2'), function($view)
{
$view->with('data', 'value');
});
Now data varaible is bound to view1 and view2. That means they it will be available every time you load these views.
Where to place them? You can place your view composers wherever you want as long as Laravel can identify it.
Read docs: View Composer
Related
I've recently started creating a layout template for my Laravel project following the method stated on here:
https://selftaughtcoders.com/from-idea-to-launch/lesson-20/creating-a-laravel-5-blade-layout-to-standardize-your-pages-appearance/
It seems easy enough to wrap a view with a 'master' layout. However, my layout includes a userbar in the top right (kind of like SO's) which has the user's avatar and username.
What is the best way to do this? I can't imagine I have to pass it through to every view that extends my master layout, but at the moment, it seems that is the only answer.
In your master layout you can simply add the user bar and wrap it in the blade helper #auth like this
#auth
This will only be shown when a user is logged in.
#endauth
Check the control structures from the documentation for more detailed information.
I think you will first need to add an separate element in some element folder. This could be the first step to perform.
Secondly, once you are done with the first step, you will also have to extend the element with #extends() function in main layout.
I hope this helps.
I have a Laravel Application with some Angular JS, I'm using Laravel to do the routing as it's easier for my team (who rarely use JS at all) to understand later, I have a route that allows me to create briefings, this is done first by selecting some options (which are passed as an array to the Laravel Controller and sent to the view) and then generating the project
Now, when I have the project generated I'm taken to another view with a sidebar, this sidebar allows me to change to different subsections of the briefing, for instance, there's a section for Development and another for Design.
These are different routes in Laravel that take the user to different forms, the problem is that these forms need to generate conditionally with blade's #if command and in order to do that, I need to get the original options array from my generator view into all of these other views.
How can this be achieved in Laravel 5.2?
Did you try to use SESSION ?
On submit a form, use SESSION to save some data that you need and you can easily find it at any moment with SESSION.
So I'm using laravel 5.1 and I'm stuck in a weird situation.
I have 2 models Seasonand Tournament and lets say the url to add a Season model is ..../seasons/add and to view the season would be ..../seasons/(id)... standard stuff right?
But now I want to add a tournament from the link ..../seasons/1 and so I click a link that takes me to ..../tournaments/add... how can I send the Season model to that page without submitting a form with hidden input?
Currently I have this setup ..../seasons/1/tournaments/add using blade to generate the links. But this method just doesn't feel right....
Thanks.
How can I send data from one page to another using Laravel?: I would suggest that you do this from your controller. Take a look at Redirecting With Flashed Session Data, it might come in handy.
From the Flash Data documentation: "[...] it will only be available during the subsequent HTTP request, and then will be deleted [...]"
You can send your model, static values or whatever you want using ->with:
redirect('route')->with('key', 'value');
I have code that I want to run on every page load, such as looking up menu items, looking up the users details etc. These will be displayed on partial views that make up the main view.
Where do I place this code so that it can fill my partial views with each page load? I know I can just add the code to the top of the partial view itself, but this doesn't really follow the MVC pattern.
Is there a function that is always called that I can hook into in my base controller?
You can create a base viewmodel for the repeated code and make other viewmodels inherit from it.
...such as looking up menu items, looking up the users details etc
You're a bit unclear about the type of information you want to load: in case the info is a view-component then indeed you should create a base-view and inherit from it or include it (composition) in any other view.
But, in case it is "user-information" - the data should live in a model-component that again, may live as "base-model" object that is included in other model components.
Well, I've read this tutorial if I could say: http://www.symfony-project.org/book/1_1/02-Exploring-Symfony-s-Code
And, actually, I write my code very similiary. But my question, where should I insert my jQuery code? I am sure it should be in the part of the View, but are there any good examples on how should I combine it to make "live" websites?
Edit: By saying live, I mean, for example, send POST request through Ajax and get information and similar. So, no refreshes for people.
Thank you.
jQuery as a part of javascript resources should be included in html.head or in-place, depending on what should jquery do and if the code is reusable for more views.
So it has to be part of View and you're choice is to set it up in layout or action view itself
If you need the javascript on every page then add it to your master view if not then just include it to the particular view files.
In context to codeigniter:
I extend the controller with MY_Controller and initialize a property as array which would hold the scripts that are added dynamically to the view.
eg.
var $templateData['scripts'] = array();
The controllers then pass $this->templateData to the views
And the views load the extra scripts( available as $scripts) as directed by the controllers in the head tag
This way you can even avoid loading the main jquery file when not needed.
Loading jquery.js only for the controller that need it.