2013-12-20 44 views
2

我有一個路線:Laravel 4從控制器重定向不起作用

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

和內部activateAccount行動我嘗試重定向到另一條路線:

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.'); 
    } 
} 

,但它不重定向,只是顯示我一個空白頁面和響應比我從Firebug->淨 - >響應是:

重新加載頁面拿到源的版本:loca lhost/myProject的/公/啓動/ 89b322bc6da6bbfd0c690c0f6bee2d2adebc9a8a

更新:當這個代碼是路線裏面正常工作。

UPDATE2:這裏是視圖

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

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

{{ Form::open(array('action' => '[email protected]', '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 

回答

1

您必須返回東西給你的應用程序:一個視圖或響應,在這種情況下,你回來從你的激活方法的響應,但沒有返回回來在控制器應用程序:

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

    return Login::activateAccount($key); 

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

而且,出於測試目的,你最好在false情況下,在這裏有一個消息太:

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.'; 
} 

在你看來,這是你如何從重定向得到你的消息:

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

我想這一點,但它是沒有問題的。當我成功激活帳戶時,我得到一個空白頁面。我可以渲染用於/ signin頁面的視圖,但是如何呈現視圖並使用with()和withErrors()方法? – xpuc7o

+0

您沒有向我們展示您的觀點,但是,我再次在此處進行了測試,您確實需要這種回報,否則您將獲得空白頁面。如果你需要使用你在視圖中使用的數據,你必須使用'Session :: get()',因爲在'Redirect'變量不會自動實例化。 –

+0

我測試了你的代碼,但沒有改變,因爲我有正確的密鑰來激活帳戶和if($ affectedRows> 0)中的代碼執行,但我仍然得到一個空白頁。我有foreach的內部視圖($ errors-> all()as $ message)

{{ $message }}
endforeach和{{Session :: get('activatedAccount')}} – xpuc7o