frenzyapp / turbolinks

Turbolinks is a direct port of the rails turbolinks gem and the jquery.turbolinks gem for projects using the Laravel 4.1+.
MIT License
163 stars 13 forks source link

Redirect does not work in Laravel 5.4 #17

Closed tranghaviet closed 6 years ago

tranghaviet commented 7 years ago

When I use redirect method but URL does not change from "/link" to "/test"

Route::get('/test', function () {
    return response()->view('test');
})->name('test');
Route::get('/link', function () {
   return redirect('/test');
});
tortuetorche commented 7 years ago

Hi @tranghaviet,

You can read these instructions to get Turbolink's redirection working.

Have a good day, Tortue Torche

tranghaviet commented 7 years ago

I read the instruction and did the following things:

  1. Add the Turbolinks middleware, to the $middleware array in app/Http/Kernel.php 'Frenzy\Turbolinks\Middleware\StackTurbolinks',

  2. Add 'Frenzy\Turbolinks\TurbolinksServiceProvider', to the providers array in config/app.php

  3. 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?

tortuetorche commented 7 years ago

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?

d13r commented 7 years ago

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.

https://github.com/helthe/Turbolinks/blob/6b798cce241769137d639cafaa5f4b919647283c/Turbolinks.php#L120-L124

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.

tortuetorche commented 7 years ago

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

https://github.com/helthe/Turbolinks/blob/6b798cce241769137d639cafaa5f4b919647283c/Turbolinks.php#L120-L124

And also https://github.com/helthe/Turbolinks/blob/6b798cce241769137d639cafaa5f4b919647283c/Turbolinks.php#L222

NB: Maybe this issue need to be moved to the https://github.com/helthe/Turbolinks repository...

Have a good day, Tortue Torche

tortuetorche commented 6 years ago

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

tortuetorche commented 6 years ago

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