Closed tranghaviet closed 6 years ago
Hi @tranghaviet,
You can read these instructions to get Turbolink's redirection working.
Have a good day, Tortue Torche
I read the instruction and did the following things:
Add the Turbolinks middleware, to the $middleware
array in app/Http/Kernel.php
'Frenzy\Turbolinks\Middleware\StackTurbolinks',
Add 'Frenzy\Turbolinks\TurbolinksServiceProvider'
, to the providers
array in config/app.php
Put this code in my app/Http/Controllers/Controller.php
file:
public function redirectTo($path, $options = [])
{
$defaultOptions = ['status' => 302, 'headers' => [], 'secure' => null];
$options = array_merge($defaultOptions, $options);
if (response()->hasMacro('redirectToWithTurbolinks')) {
dd('fasdfa');
return response()->redirectToWithTurbolinks($path, $options);
}
return response()->redirectTo($path, $options['status'], $options['headers'], $options['secure']);
}
And my routes/web.php
:
Route::get('/test', function () {
return view('test');
});
Route::get('/link', 'AppController@index');
My AppController.php file
:
class AppController extends Controller{
function index(Request $request)
{
return response()->redirectTo('/test');
// return response()->redirectToWithTurbolinks('/test');
}
}
My test
view have an anchor link to /test
and already has turbolinks.js
.
Either redirectTo('/test')
and redirectToWithTurbolinks('/test')
isn't working.
What I'm missing?
Hi @tranghaviet,
You should remove the dd('fasdfa');
line.
Can you send me the response (text, HTTP status code, type, headers...) of the failing redirection, please?
I have the same issue in Laravel 5.5. I think I've tracked it down to underlying library expecting the session to be an instance of Symfony\Component\HttpFoundation\Session\SessionInterface
not Illuminate\Contracts\Session\Session
.
The "Sessions" section of the Upgrade Guide says:
Laravel's session handlers no longer implements Symfony's
SessionInterface
. ... All calls to the->set()
method should be changed to->put()
.
This results in the Turbolinks-Location
header not being set.
I'm not sure what the best solution is though... Perhaps replace the StackTurbolinks
with one that always adds the header, instead of relying on the session:
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
$response = $this->app->handle($request, $type, $catch);
if (self::MASTER_REQUEST === $type) {
$this->turbolinks->decorateResponse($request, $response);
$response->headers->set('Turbolinks-Location', $request->fullUrl()); // <-- Added
}
return $response;
}
That seems to work for me anyway.
Hi @davejamesmiller,
You're right I need to investigate in the Turbolinks.php
file to support both Symfony\Component\HttpFoundation\Session\SessionInterface
and Illuminate\Contracts\Session\Session
interfaces
NB: Maybe this issue need to be moved to the https://github.com/helthe/Turbolinks repository...
Have a good day, Tortue Torche
Hi,
After some tests, it works as expected in some conditions...
Turbolinks correctly handles redirection if the current request is an XHR (AJAX) one and hasn't a GET HTTP method.
See: https://github.com/turbolinks/turbolinks-rails/blob/v5.0.1/lib/turbolinks/redirection.rb#L13 And https://github.com/helthe/Turbolinks/blob/6b798cce241769137d639cafaa5f4b919647283c/Turbolinks.php#L100
I need more times for the session stuff...
Cheers, Tortue Torche
Hi folks,
The 3.2.0 release should resolve this issue.
You can edit your composer.json
file to bump turbolinks
version constraint, like this:
{
"require": {
// ...
"frenzy/turbolinks": "~3.2.0"
}
}
Have a good day, Tortue Torche
When I use redirect method but URL does not change from
"/link"
to"/test"