2014-10-31 57 views
1

我正在使用Laravel和Moltin/Cart構建在線商店的購物車系統。到目前爲止,我已經設法整合購物車系統並繼續使用PayPal。 雖然,在我的控制器中,我有一個構造函數,可以防止用戶查看,添加或刪除購物車中的物品,除非經過身份驗證。Laravel在線購物車

public function __construct() { 
    parent::__construct(); 
    $this->beforeFilter('csrf', array('on'=>'post')); 
    $this->beforeFilter('auth', array('only'=>array('postAddtocart', 'getCart', 'getRemoveitem'))); 
} 

爲了使貝寶工作,我不得不加在我看來幾個隱藏的輸入,獲得必要的值,並將其傳遞給PayPal的支付頁面,就像這樣:

<input type="hidden" name="cmd" value="_xclick"> 
<input type="hidden" name="business" value="[email protected]"> 
<input type="hidden" name="item_name" value="eCommerce Store Purchase"> 
<input type="hidden" name="currency_code" value="EUR"> 
<input type="hidden" name="amount" value="{{ Cart::total() }}"> 
<input type="hidden" name="first_name" value="{{ Auth::user()->firstname }}"> 
<input type="hidden" name="last_name" value="{{ Auth::user()->lastname }}"> 
<input type="hidden" name="email" value="{{ Auth::user()->email }}"> 

{{ HTML::link('/', 'Continue Shopping', array('class'=>'btn btn-default')) }} 
<input type="submit" value="Checkout with Paypal" class="btn btn-primary"> 

的問題是用戶不應登錄查看,添加或刪除購物車中的物品,但在點擊提交按鈕時要求登錄。如果我從構造函數中刪除過濾器,那麼由於使用Auth類的隱藏輸入,我得到了「嘗試獲取非對象的屬性」錯誤。我試圖添加一個刀片,如果驗證::檢查條件,但這不是解決方案。有什麼建議麼?

回答

這奏效了:

@if(!Auth::check()) 

     {{ HTML::link('users/signin', 'Sign in to pay', array('class'=>'btn btn-primary')) }} 

@else 
<input type="hidden" name="first_name" value="{{ Auth::user()->firstname }}"> 
<input type="hidden" name="last_name" value="{{ Auth::user()->lastname }}"> 
<input type="hidden" name="email" value="{{ Auth::user()->email }}"> 

{{ HTML::link('/', 'Continue Shopping', array('class'=>'btn btn-default')) }} 
<input type="submit" value="Checkout with Paypal" class="btn btn-primary"> 
@endif 

另外,我從構造去除過濾器,改變了重定向在功能標誌,以便它重定向到先前訪問頁。

使用一個會話變量來保存以前訪問過的網址

爲了回去,您需要使用session::put以前訪問過的頁面。

所以,在getSignin功能添加:

Session::put('previous_url', URL::previous()); 

而在postSignin功能,您需要檢索previous_url變量,它存儲在會話中,像這樣:

if (Session::has('previous_url')) 
{ 
    $url = Session::get('previous_url'); 
    Session::forget('previous_url'); 
    return Redirect::to($url); 
} 
+0

嗨,我正在研究相同的電子商務laravel教程,並且執行相同的邏輯。在那裏,你是如何做到重定向的目的是在登錄後頁面返回到購物車的地方。我似乎無法弄清楚它的正確性。謝謝。 – Bobby 2014-11-24 10:10:07

+0

成功登錄後,您也可以使用laravel的「重定向::預期('/')」重定向技巧。 – Cyruxx 2015-02-24 21:33:40

回答

4

如何關於用登錄按鈕替換結賬按鈕,然後再次重定向到購物車結賬?