Laravel has no idea you're using something like PJAX.
You should include @extends('layout.main')
in each route. In order to work with PJAX, you should make layout.main
only spit out the site layout if Request::ajax()
is false.
Frage
Currently integrating jquery pjax, will do my best to explain. I have it working inside the first view however when accessing the URL address directly from address bar I get the view without the @extends('layout.main')
.
Here is my setup:
Routes, one route is for the retailers.index
view and the other is for my GET parameter and retailers.stores
view.
Route::get('retailers', array(
'as' => 'retailers',
'uses' => 'SiteController@retailers'));
Route::get('retailers/{city}', array(
'as' => 'find-retailers',
'uses' => 'SiteController@getRetailers'));
Controllers:
public function retailers() {
$listings = DB::table('retailers_listings')
->orderBy('counter', 'desc')
->groupBy('country')
->get();
return View::make('retailers.index')
->with('retailers_listings', $listings)
}
public function getRetailers($city) {
$locations = DB::table('retailers_listings')
->orderBy('country', 'asc')
->Where('city', $city)
->get();
return View::make('retailers.stores')
->with('retailers_listings', $locations);
}
href with pjax and URL route to retailers.stores
view - using find-retailers
<a data-pjax href="{{ URL::route('find-retailers', array('city' => $string)) }}">
{{ $string }}
</a>
Inline JS
$(document).pjax('[data-pjax] a, a[data-pjax]', '#pjax-container')
If I am loading /retailers (index) everything works and if I press the link it will load store.retailers
view inside the <div id="pjax-container"></div>
and the URL will change to retailers/{country}. If I load from the URL address bar directly it will only load the view without @extends('layout.main')
, as I do not call that in my retailers.stores
view because I load retailers.stores in pjax.
Basically it works, but not when loading directly for address bar.
Lösung 2
Laravel has no idea you're using something like PJAX.
You should include @extends('layout.main')
in each route. In order to work with PJAX, you should make layout.main
only spit out the site layout if Request::ajax()
is false.
Andere Tipps
You should NOT use @if(!Request::ajax())
to determine if the request is by PJAX.
PJAX will provide specific custom headers (HTTP_X_PJAX
and HTTP_X_PJAX_CONTAINER
) that you should be using to determine what the request is asking for.
@if($request->server->get('HTTP_X_PJAX'))
For a simpler solution...
You should take a look at this laravel package for working with PJAX:
https://github.com/simonstamm/laravel-pjax
It uses the headers mentioned above properly and will even automatically send back the appropriate part of a view for a request.