Question

For some reason I can't understand, when I'm using the Redirect::intended() method after login I'm always sent to the fournisseurs/ax_getListProduits URL of my routes file, whatever is my real intended URL, with 2 exceptions:

  • if I remove the post fournisseurs/ax_getListProduits route, everything is OK
  • if I add a echo Session::get('url.intended'); call before the login page display, the correct intended URL is displayed, and it works (but if I add this code after Auth::attempt, I'm sent to fournisseurs/ax_getListProduits).

I can't understand what's happening… I can't find any similar problem here or in the Laravel Github repository, which leads me to think I'm doing something wrong, but I can't find it… I posted this question as a possible bug on Github, but without success, here : https://github.com/laravel/framework/issues/2668.

Below are my routes file and my login controller code:

routes.php

Route::group(array('before' => 'auth'), function() {
  Route::get('/', array("as"=>"home", function() {
    return Redirect::to("fournisseurs");
  }));
  Route::resource('usergroups', 'UsergroupsController');
  Route::get('fournisseurs/ax_produits', 'FournisseursController@ax_produits');
  Route::post('fournisseurs/ax_getProduit', 'FournisseursController@ax_getProduit');
  Route::post('fournisseurs/ax_updProduit', 'FournisseursController@ax_updProduit');
  Route::post('fournisseurs/ax_getListProduits', 'FournisseursController@ax_getListProduits');
  Route::resource('fournisseurs', 'FournisseursController');

  Route::resource('adresses', 'AdressesController', array('only' => array('store', 'destroy', 'edit', 'update')));
  Route::get('/adresses/create/{frs_id}', array('as'=>'adresses.create', 'uses'=>'AdressesController@create'));

  Route::resource('contacts', 'ContactsController', array('only' => array('store', 'destroy', 'edit', 'update')));
  Route::get('/contacts/create/{frs_id}', array('as'=>'contacts.create', 'uses'=>'ContactsController@create'));
});

Route::get('login', array('as' => 'login', 'uses' => 'SessionsController@create'));
Route::get('logout', array('as' => 'logout', 'uses' => 'SessionsController@destroy'));
Route::resource('sessions', 'SessionsController', array('only' => array('create', 'store', 'destroy')));

Login controller:

class SessionsController extends BaseController {

public function create() {
    // if I uncomment the following line, everything is OK
    //  echo Session::get('url.intended');
    return View::make('sessions.create');
}

public function store() {
    $input = Input::all();

    $attempt = Auth::attempt(array(
        "nom" => $input["nom"],
        "password" => $input["password"]
    ));

    if($attempt) return Redirect::intended("/");

    return Redirect::back()->with("flash_error", "Nom ou mot de passe invalide")->withInput();
}

public function destroy() {
    Auth::logout();
    return Redirect::home();
}

}

view create.blade

@extends("layouts.default")
@section("content")

<div class="login_form">

<div class="row"><div class="col-md-4 col-md-offset-4">
  <h2>Identification</h2>
</div></div>

<div class="row">
  <div class="col-md-4 col-md-offset-4">
    {{ Form::open(array('route' => 'sessions.store', 'class' => 'form', 'role' => 'form')) }}
      <div class="form-group">
        {{ Form::label('nom', 'Nom') }}
        {{ Form::text('nom', '', array('autocomplete'=>'off', 'class' => 'form-control')) }}
      </div>
      <div class="form-group">
        {{ Form::label('password', 'Mot de passe') }}
        {{ Form::password('password', array('autocomplete'=>'off', 'class' => 'form-control')) }}
      </div>
      {{ Form::submit("Connexion", array("class"=>"btn btn-primary")) }}
      {{ Form::close() }}
    </div>
  </div>

</div>

@stop

And default.blade layout

<!DOCTYPE html>
<html lang="fr">
<head>
    (...) head removed for code clarity
</head>
<body>
    <div class="container">

        @if(Session::get("flash_msg"))
            <div class="row fox-info"><div class="col-md-12">
                <div class="alert alert-info">
                    {{ Session::get("flash_msg") }}
                </div>
            </div></div>
        @endif

        <nav class="navbar navbar-default" role="navigation">
            <a class="navbar-brand" href="#">EK | FOX</a>

            <ul class="nav navbar-nav">
                @yield("trail")
            </ul>

            <ul class="nav navbar-nav navbar-right">
                @if(Auth::user())
                <li class="dropdown">
                    <a href="#" data-toggle="dropdown">{{ Auth::user()->nom }} <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li><a href="{{ route('logout') }}">Déconnexion</a></li>
                    </ul>
                </li>
                @endif
            </ul>
        </nav>

        @if(Session::get("flash_error"))
            <div class="row"><div class="col-md-12">
                <div class="alert alert-danger">
                    {{ Session::get("flash_error") }}
                </div>
            </div></div>
        @endif

        @yield("content")
    </div>

    (...) js scripts removed for code clarity

</body>
</html>
Was it helpful?

Solution

I found some time to look after this issue, and the problem came from an unfortunate ajax post call to the "unwanted route" (fournisseurs/ax_getListProduits). This ajax call was made after the page creation, and even if it returns a 404 error, Laravel was updating the url.intended Session variable between page creation and form submission.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top