Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect does not work in Laravel 5.4 #17

Closed
tranghaviet opened this issue Aug 27, 2017 · 7 comments · Fixed by #18
Closed

Redirect does not work in Laravel 5.4 #17

tranghaviet opened this issue Aug 27, 2017 · 7 comments · Fixed by #18

Comments

@tranghaviet
Copy link

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
Copy link
Contributor

tortuetorche commented Aug 28, 2017

Hi @tranghaviet,

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

Have a good day,
Tortue Torche

@tranghaviet
Copy link
Author

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
Copy link
Contributor

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
Copy link

d13r commented Oct 23, 2017

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
Copy link
Contributor

tortuetorche commented Oct 24, 2017

@tortuetorche
Copy link
Contributor

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 added a commit to tortuetorche/Turbolinks that referenced this issue Dec 13, 2017
> Laravel's session handlers no longer implements Symfony's `SessionInterface`.
> ...
> All calls to the `->set()` method should be changed to `->put()`.

See frenzyapp/turbolinks#17
tortuetorche added a commit to tortuetorche/Turbolinks that referenced this issue Dec 21, 2017
> Laravel's session handlers no longer implements Symfony's `SessionInterface`.
> ...
> All calls to the `->set()` method should be changed to `->put()`.

See frenzyapp/turbolinks#17
carlalexander pushed a commit to helthe/Turbolinks that referenced this issue Jan 4, 2018
> Laravel's session handlers no longer implements Symfony's `SessionInterface`.
> ...
> All calls to the `->set()` method should be changed to `->put()`.

See frenzyapp/turbolinks#17
tortuetorche added a commit to efficiently/turbolinks that referenced this issue Jan 8, 2018
@tortuetorche
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants