Reputation: 504
This is an extension from a post I made a few days ago: Change a page without refreshing - Laravel / Ajax
Basically, I'm trying to replicate the URL Structure of Soundcloud where you can click on a link, it'll load the content without refreshing the page however if you land directly on that page, it won't replicate design and effecitvely break.
I've been thinking of ways on how I can check in Laravel if the page is requested via Ajax or has been landed on without an Ajax call.
What's happening at the moment is that when I call the page, the view has a master template that's extended thus creating duplicate master templates on the one view.
I was thinking if I done something like
@if(!Request::ajax())
@extends('masterlayout')
@endif
It would work but tried and no luck.
Any help as always is greatly appreciated!
Thanks
Upvotes: 1
Views: 481
Reputation: 504
Thanks for all the answers, I eventually got this fixed by using @if(!Request::ajax())
on section that didn't need to be shown when the page was loaded via ajax.
Thank you again! :D
Upvotes: 0
Reputation: 6662
Looks like @extends
directive is always executed even in falsy if
You can add an empty layout and perform check like this:
@extends(Request::ajax()? 'layouts.empty' : 'layouts.master')
and you need add only this in layout empty.blade.php
:
@yield('content')
or you can add ajax check in the master layout
@if(!Request::ajax())
// all layout code
@else
@yield('content')
@endif
Upvotes: 3
Reputation: 5164
You can check that in your controller action.
public function index(Request $request)
{
if($request->ajax()){
return "This is an AJAX call!";
}
return "Not an AJAX call...";
}
Upvotes: 1