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');