Laravel - How to NOT use a layout - php

I'm using laravel with controllers layout. But there are some parts of my app where I don't want to use a layout (for example, when returning data to the payment gateway request, for wich I send XML data). I just want to pass data to my view and render it alone, with no need for a layout.
How can I do that? I've been trying some approaches but none worked for this. I can successfuly change what layout to render, but I can't set to render the view without a layout.
Thanks!
Edit: Let me explain it better
My default layout is set in Base_Controller. Then all my controllers extends it but in one of them I need no layout, as I told above. Maybe I need to unset the default layout or something like that, I'm not sure.

You can simply return something from your controller action to bypass the layout.
function get_xml($id) {
$user = User::find($id);
return View::make('user.xml', $user);
}

On your controller functions, you can simply return a string, which will be thrown back to the browser as-is. Alternatively, you can craft a Laravel\Response object, which will allow you to fine-tune your site's output a lot more than just returning a string.
The Response class has a few tricks up its sleeve that are not mentioned on the docs: default return, JSON, forced download.
You're more interested in the first one, which will allow you to correctly set the content-type of the response to application/xml. In addition to this, you can still use views for XML! Generate the view as you would with View::make, but instead of directly returning it, store it in a variable. To render it, call render() on it - it will return the output.

A simple way....
suppose there is a main layout
<body>
#yield('content')
</body>
This content will be where the view will be inserted.
Now,
if you want to use layout, Make the view page like this:
#layout('main')
#section('content')
blah blah your content
#endsection
If you don't want to use layout, omit the codes above.
In controller, the code will be same for both the files.
return View::make('index');

Related

Laravel - How to pass data to include

So essentially all of my views are using the header.blade.php since I am including it in my master layout. I need to pass data to the header on every single view. Is there a way to pass data just to the include rather than passing the data for the header in each view?
You don't need to do that, but you can:
All variables that are available to the parent view will be made
available to the included view. Even though the included view will
inherit all data available in the parent view, you may also pass an
array of extra data to the included view:
#include('view.name', ['some' => 'data'])
One option if you're trying to send data only to the included view is to use a view composer. They will fire even in the case of trying to prepare a view for #include
view()->composer('header', function($view) {
$view->with('data', 'some data');
});
actually the very best and faster method of sharing data to all views could be just using the
AppServiceProvider
instead of Jeff's answer you can use the share method instead of the composer method and achieve your goal faster.
Just pass the data you want in the boot method of the AppServiceProvider like following
public function boot()
{
View::share('key', 'value');
}
for more check this

Laravel - Provider data being requested on all routes

I am currently using a Laravel Provider to pass data to a view each time it is called. The App.blade.php includes the blade file if the user is authenticated.
My problem is that at the moment, no matter what view the user is on, it still calls the ViewServiceProvider.php, which doesn't seem very efficient.
I have tried to use #if(view()->exists('home')), but that doesn't seem to have any effect what so ever, and thus, the queries are still called from the ViewServiceProvider.php.
App.blade.php:
#if(!Auth::guest())
#if(view()->exists('home'))
#include('layouts.check')
#endif
#endif
ViewServiceProvider.php:
public function boot()
{
view()->composer('layouts.check', function ($view) {
$sites = Site::where('trust_id', Auth::id())->get();
$view->with(['sites' => $sites]);
});
}
Any help would be hugely appreciated.
The problem with you code is that it's check to see if the view exists. Chances are, the view "home" will always exist, so it will always include your view; "layouts.check".
Unless, of course, the view "home" is dynamic and is only there conditionally, which doesn't seem right. If you want the "layouts.check" view file to only load on certain pages, you might want to try "Request::is()".
#if(Request::is('home'))
#include('layouts.check')
#endif
The view composer is going to be called whenever your 'layouts.check' view is rendered.
Even though you've attempted to not have it rendered (by adding in your if statements), the view is still going to be rendered, and your view composer is still going to be called.
The template engine is going to parse your view in one pass. The engine doesn't care about any type of logic inside you're view, it's only job is to convert that logic into PHP code. So, even though you have the statement #if(!Auth::guest()), the parser doesn't understand the actual logic, it just knows to convert that to <?php if (!Auth::guest()) { </php>.
Basically, your #if statements aren't preventing the engine from parsing your include file, it is the parsed PHP code that prevents the results of the include file from showing in your output. So, since your #include file is parsed, the view composer is called.

Pass variable from layout to view in ZF

I'm trying to pass a variable from the master layout to the view, but I'm having no luck.
layout.phtml
$this->hadMessages = true;
myview.phtml
var_dump($this->hadMessages);
The var_dump always comes back with NULL. From what I've read, the layout is a view too, so it should be in the same context, right?
I'm using Zend Framework 1.11.
The layout is rendered after the view, so that's why this doesn't work. Depending on what you are trying to do you might be able to achieve the desired effect with the help of a controller plugin.
There is no way to pass variable from layout to view, because according to MVC pattern you shouldn't have tasks like this. Wether layout contains some messages or not should be decided on controller or bootstrap level, not in layout itself.
It means in controller you should make all needed assignments like $this->view->layout_messages_shown = true and get this variable value both in layout and view like echo ( $this->layout_messages_shown ? "messages shown" : "messages hidden" )

Injecting random variable into every View in Laravel

I am just getting started with Laravel and working on porting a mess of a site to the framework.
One feature of the site is a dynamically added image in the header. I am using a common Blade template and was wondering if there is any way to inject a random variable (an integer between 1 and 4 would do) into every View that uses that layout.
What I would like to do is to be able to add something like so in the the common template-
<img src="img/cutouts/cutout-<?= $randomInt;?>.jpg" alt=""/>
with $randomInt sent to every View
You could look into View composers
So you would have something like:
View::composer('your.view', function($view)
{
$view->with('randomInt', rand(1,4));
}
That will pass the $randomInt variable in everytime you use the 'your.view' (or whatever) View.
It's also possible to add a variable to all views through View::share().
For example, you could modify the __construct method in Base_Controller with:
View::share('randomInt', rand(1,4));

What is Layout and what is View in ZF? When and whose variables should I use and why?

I can't understand when to use Layout's variables and when to use View's variables to get page segments on the page. Here is the picture form their Layout package tutorial ($this means the View instance everywhere):
Why Navigation, Content and Sidebar segments are got as Layout variables?
$this->layout()->nav;
But HeadTitle, HeadScript, HeadStylesheet are got straightly from View?
$this->headTitle(); // I know that this is a placeholder view helper.
// But this segment of the page logically belongs to Layout.
// and it has to be called smth like view->layout->placeholder
And why Header and Footer are from some partial method of the View but not Layout's properties?
$this->partial('header.phtml');
I've tried to change them and both ways work fine:
echo $this->nav; // I assigned navigation segment script to the View and it works;
I tried to assign Footer segment script to the Layout and it also works:
$layout->footer = $footer;
echo $this->layout()->footer; // it also works, it's displayed on the page
Any of the ways may be applied to any variable on the page. For example in Navigation segment I have a lot of variables to display and I can output them using both ways - one variable as Layout's property, another one sa View's property.
So what is the rule to use them right way? When should I use View's variables and when Layout's ones?
I agree that this isn't very clear from the documentation, and I don't think $this->layout()->nav is explained at all. A few points that might help:
$this->layout() is actually a call to the layout view helper, which returns the current instance of Zend_Layout.
Zend_Layout registers its own placeholder helper (with the key 'Zend_Layout'), and by default creates a 'content' variable in this.
the Zend_Layout class has a magic __get() method which proxies any member variable calls over to its registered placeholder container. So calling $this->layout()->content is another way of writing $this->placeholder('Zend_Layout')->content
the Zend_Layout class also has a magic __set() method that proxies stored data to the placeholder class. So $layout->footer = 'foo' is the same as calling $this->placeholder('Zend_Layout')->footer = 'foo' in the view
With that in mind:
Why Navigation, Content and Sidebar segments are got as Layout variables?
As these are accessing data stored in Zend_Layout's placeholder. You could also use $this->placeholder('Zend_Layout')->content
But HeadTitle, HeadScript, HeadStylesheet are got straightly from View?
These are view helpers.
And why Header and Footer are from some partial method of the View but not Layout's properties?
This is the standard way of accessing content from other templates.
In general, assume that using the view object is the correct way to access the data. Use the layout object/helper only if you know the data is in the layout placeholder.
The advantage of using placeholders over partials is that you can access and modify them in several different places, including in the view itself. For example say you had a sidebar which is stored in a partial. If you were to store this in the Zend_Layout placeholder instead (for example in a controller plugin), you can then override this for certain actions in the controller:
public function someAction()
{
$this->view->layout()->sidebar = 'Some other sidebar content';
}
or in the view script itself:
<?php $this->layout()->sidebar = 'Content for this page only'; ?>

Categories