xpuc7o
xpuc7o

Reputation: 333

Laravel 4 redirect from controller does not work

I have a route:

Route::get('activate/{key}', function($key) {
        Login::activateAccount($key);
    })->where('key', '[a-z0-9]{40}');

and inside activateAccount action I try to redirect to another route:

public static function activateAccount($key) {        
    $affectedRows = User::where('activation_code', '=', $key)->update(array('is_active' => 1));

    if ($affectedRows > 0) {
        return Redirect::to('/signin')->with('activatedAccount', 'Your account is activated. You can sign in now.');
    }
}

but it does not redirect, just show me a blank page and the response than I get from Firebug->Net->Response is:

Reload the page to get source for: localhost/myProject/public/activate/89b322bc6da6bbfd0c690c0f6bee2d2adebc9a8a

Update: when this code is inside the Route it works normally.

Update2: here is the view

<div>
    {{ Session::get('activatedAccount') }}

    {{ Session::get('serverError') }}
</div>

{{ Form::open(array('action' => 'Login@signIn', 'method' => 'post')) }}

<input type="text"
       name="email"
       placeholder="Email" />

<input type="password"
       name="password"
       placeholder="Password" />

<input type="submit"
       value="Sign In" />

{{ Form::close() }}

{{ link_to('choose_signup', 'Sign Up', $secure = null) }}

@foreach ($errors->all() as $message)
<div>
    {{ $message }}
</div>
@endforeach

Upvotes: 2

Views: 4092

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

You must return something to you application: a view or a response, in this case you're returning a response from your activation method, but not returning it back to your application in the controller:

Route::get('activate/{key}', function($key) {

    return Login::activateAccount($key);

})->where('key', '[a-z0-9]{40}');

And, for testing purposes, you better have a message here in case of false too:

public static function activateAccount($key) {        
    $affectedRows = User::where('activation_code', '=', $key)->update(array('is_active' => 1));

    if ($affectedRows > 0) {
        return Redirect::to('/signin')->with('activatedAccount', 'Your account is activated. You can sign in now.');
    }

    return 'User was not activated.';
}

In your view this is how you get your message from that redirect:

{{ Session::get('activatedAccount') }}

Upvotes: 1

Related Questions