還是拿來主義編程思想,既然別人實現了,就不再造輪子了。
`LaravelShoppingcart`是Laravel下的一個購物車實現,項目地址:
> https://github.com/Crinsane/LaravelShoppingcart
文檔翻譯:
針對Laravel 4的簡單購物車實現。
## 安裝
通過composer安裝,編輯 `composer.json` 文件,如下:
#### Laravel4.2及以下
~~~
"require": {
"laravel/framework": "4.2.*",
"gloudemans/shoppingcart": "~1.2"
}
~~~
#### Laravel5
~~~
"require": {
"laravel/framework": "5.0.*",
"gloudemans/shoppingcart": "dev-master"
}
~~~
#### 接著運行更新命令
~~~
composer update
~~~
添加以下代碼到 `app/config/app.php` 的 `service providers` 數組里
~~~
'Gloudemans\Shoppingcart\ShoppingcartServiceProvider'
~~~
添加以下代碼到`aliases`數組中
~~~
'Cart' => 'Gloudemans\Shoppingcart\Facades\Cart',
~~~
完成以上工作就可以在你的項目中使用了。
## 使用
Shoppingcart提供了以下的可用方法:
#### Cart:add()
~~~
/**
* Add a row to the cart
* 向購物車添加新行
*
* @param string|Array $id Unique ID of the item|Item formated as array|Array of items
* -參數 字符串|數組 $id item的唯一id | Item格式化成數組 | items 數組
* @param string $name Name of the item
* -參數 字符串 $name item 名稱
* @param int $qty Item qty to add to the cart
* -param int $qty 添加到購物車的item的數量
* @param float $price Price of one item
* -param float $price item 價格
* @param Array $options Array of additional options, such as 'size' or 'color'
* @param Array $options 附加參數數組, 諸如'size' 或 'color'
*/
// Basic form 基礎表單
Cart::add('293ad', 'Product 1', 1, 9.99, array('size' => 'large'));
// Array form 數組表單
Cart::add(array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => array('size' => 'large')));
// Batch method 批量方法
Cart::add(array(
array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00),
array('id' => '4832k', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00, 'options' => array('size' => 'large'))
));
~~~
#### Cart:update()
~~~
/**
* Update the quantity of one row of the cart
*
* @param string $rowId The rowid of the item you want to update
* @param integer|Array $attribute New quantity of the item|Array of attributes to update
* @return boolean
*/
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
Cart::update($rowId, 2);
OR
Cart::update($rowId, array('name' => 'Product 1'));
~~~
#### Cart::remove()
~~~
/**
* Remove a row from the cart
*
* @param string $rowId The rowid of the item
* @return boolean
*/
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
Cart::remove($rowId);
~~~
#### Cart::get()
~~~
/**
* Get a row of the cart by its ID
*
* @param string $rowId The ID of the row to fetch
* @return CartRowCollection
*/
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
Cart::get($rowId);
~~~
#### Cart::content()
~~~
/**
* Get the cart content
*
* @return CartCollection
*/
Cart::content();
~~~
#### Cart::destroy()
~~~
/**
* Empty the cart 清空購物車
*
* @return boolean
*/
Cart::destroy();
~~~
#### Cart:total()
~~~
/**
* Get the price total 獲取總價格
*
* @return float
*/
Cart::total();
~~~
#### Cart:count()
~~~
/**
* Get the number of items in the cart
*
* @param boolean $totalItems Get all the items (when false, will return the number of rows)
* @return int
*/
Cart::count(); // Total items
Cart::count(false); // Total rows
~~~
#### Cart::search()
~~~
/**
* Search if the cart has a item
*
* @param Array $search An array with the item ID and optional options
* @return Array|boolean
*/
Cart::search(array('id' => 1, 'options' => array('size' => 'L'))); // Returns an array of rowid(s) of found item(s) or false on failure
~~~
#### Collections
顯而易見,`Cart::content()`和`Cart::get()`返回一個集合,`CartCollection`和`CartRowCollection`。
這些集合擴展了原始的`Laravel 4`的`collection`類,所以這個類的所有方法同樣的可以被使用在購物車類中。
## 實例
現在擴展包可以支持多個購物車實例,工作方式像這樣:
你可以使用`Cart::instance(‘newInstance’)`設置當前的購物車實例,此刻,有效的購物車實例是`newInstance`,所以你添加、移除或獲取購物車內容,操作的都是`newInstance`。如果你需要選擇其它實例,你只需要調用`Cart::instance(‘otherInstance’)`
~~~
Cart::instance('shopping')->add('192ao12', 'Product 1', 1, 9.99);
// Get the content of the 'shopping' cart
Cart::content();
Cart::instance('wishlist')->add('sdjk922', 'Product 2', 1, 19.95, array('size' => 'medium'));
// Get the content of the 'wishlist' cart
Cart::content();
// If you want to get the content of the 'shopping' cart again...
Cart::instance('shopping')->content();
// And the count of the 'wishlist' cart again
Cart::instance('wishlist')->count();
~~~
> 備注1:如果你不重新設置,購物車永遠是最后一個實例
> 備注2:默認的購物車實例名稱是mian,所以Cart::content();等同于Cart::instance(‘main’)->content()。
## Models
一個新特性是可以使為購物車的item關聯一個模型。假設在你的應用中有一個product模型,通過新的`associate()`方法,可以通知購物車里的項關聯到product模型。
這樣你可以從 `CartRowCollection` 訪問你的模型。
下面是一個示例:
~~~
<?php
/**
* Let say we have a Product model that has a name and description.
*/
Cart::associate('Product')->add('293ad', 'Product 1', 1, 9.99, array('size' => 'large'));
$content = Cart::content();
foreach($content as $row)
{
echo 'You have ' . $row->qty . ' items of ' . $row->product->name . ' with description: "' . $row->product->description . '" in your cart.';
}
~~~
訪問模型的主鍵和關聯模型的名稱相同。associate()還有第二個參數,是可選的,用于指定模型的命名空間。
## 異常錯誤
當發生錯誤時,購物車模塊將拋出異常。使用購物車模塊可以很容易的調試錯誤,或者基于異常的類型處理錯誤。購物車模塊可以拋出以下異常:
|Exception|Reason|
|---------|------|
|ShoppingcartInstanceException|When no instance is passed to the instance() method 沒有實例傳遞給instance()方法|
|ShoppingcartInvalidItemException|When a new product misses one of it’s arguments (id,name, qty, price) 當新產品缺少一個參數|
|ShoppingcartInvalidPriceException|When a non-numeric price is passed當非數值的價格被傳遞|
|ShoppingcartInvalidQtyException|When a non-numeric quantity is passed當非數值的數量被傳遞|
|ShoppingcartInvalidRowIDException|When the $rowId that got passed doesn’t exists in the current cart當當前的購物車并不存在被傳來的$roeid|
|ShoppingcartUnknownModelException|When an unknown model is associated to a cart row當一個未知的模型被關聯到購物車的行|
## Events
下面是購物車監聽的事件:
|Event|Fired|
|-----|-----|
|cart.add($item)|When a single item is added當單個item被添加|
|cart.batch($items)|When a batch of items is added當一批item被添加|
|cart.update($rowId)|When an item in the cart is updated購物車單個item跟更新|
|cart.remove($rowId)|When an item is removed from the cart當一個item被從購物車中移除|
|cart.destroy()|When the cart is destroyed當購物車被清空|
#### 示例:
下面的示例展示了如果在table中呈現購物車的內容
~~~
// Controller
Cart::add('192ao12', 'Product 1', 1, 9.99);
Cart::add('1239ad0', 'Product 2', 2, 5.95, array('size' => 'large'));
// View
<table>
<thead>
<tr>
<th>Product</th>
<th>Qty</th>
<th>Item Price</th>
<th>Subtotal</th>
</tr>
</thead>
<tbody>
<?php foreach($cart as $row) :?>
<tr>
<td>
<p><strong><?php echo $row->name;?></strong></p>
<p><?php echo ($row->options->has('size') ? $row->options->size : '');?></p>
</td>
<td><input type="text" value="<?php echo $row->qty;?>"></td>
<td>$<?php echo $row->price;?></td>
<td>$<?php echo $row->subtotal;?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
~~~
* * * * *
原文地址:[http://www.zhangxihai.cn/archives/189](http://www.zhangxihai.cn/archives/189)
- 前言
- 發行說明/L5新特性
- 升級向導
- 升級到 5.0.16
- 從 4.2 升級到 5.0
- 從 4.1 升級到 4.2
- 從 4.1.x 升級到 4.1.29
- 從 4.1.25 升級到 4.1.26
- 從 4.0 升級到 4.1
- 貢獻向導
- 環境配置
- 安裝
- 配置
- 基本功能
- 路由
- 基本路由
- CSRF 保護
- 方法欺騙
- 路由參數
- 命名路由
- 路由群組
- 路由模型綁定
- 拋出 404 錯誤
- 中間件
- 建立中間件
- 注冊中間件
- 可終止中間件
- 控制器
- 基礎控制器
- 控制器中間件
- 隱式控制器
- RESTful 資源控制器
- 請求
- 取得請求實例
- 取得輸入數據
- 舊輸入數據
- Cookies
- 上傳文件
- 其他的請求信息
- 響應
- 基本響應
- 重定向
- 其他響應
- 響應宏
- 系統架構
- 服務提供者
- 基本提供者例子
- 注冊提供者
- 緩載提供者
- 服務容器
- 基本用法
- 將接口綁定到實現
- 上下文綁定
- 標簽
- 實際應用
- 容器事件
- 參考:理解PHP 依賴注入|Laravel IoC容器
- Contracts
- 為什么用 Contracts
- Contract 參考
- 如何使用 Contracts
- Facades
- 實際用法
- 建立 Facades
- 模擬 Facades
- Facade 類參考
- 請求的生命周期
- 生命周期概要
- 聚焦于服務提供者
- 應用程序結構
- 根目錄
- App 目錄
- 為應用程序配置命名空間
- 系統服務
- 認證
- 用戶認證
- 取得經過認證的用戶
- 保護路由
- HTTP 基本認證
- 忘記密碼與重設
- 第三方登陸認證
- 交易
- 配置文件
- 訂購方案
- 一次性付款
- Single Charges
- 免信用卡試用
- 訂購轉換
- 訂購數量
- 取消訂購
- 恢復訂購
- 確認訂購狀態
- 處理失敗訂閱
- 處理其它 Stripe Webhooks
- 收據
- 緩存
- 配置
- 緩存用法
- 遞增與遞減
- 緩存標簽
- 緩存事件
- 數據庫緩存
- 集合
- Command Bus
- 建立命令
- 調用命令
- 命令隊列
- 命令管道
- 核心擴展
- 管理者和工廠
- 緩存
- Session
- 認證
- 基于服務容器的擴展
- Laravel Elixir
- 安裝與配置
- 使用方式
- Gulp
- Custom Tasks and Extensions
- 加密
- Envoy 任務執行器
- 安裝
- 執行任務
- 多服務器
- 并行執行
- 任務宏
- 通知
- 更新 Envoy
- 錯誤與日志
- 配置
- 錯誤處理
- HTTP 異常
- 日志
- 事件
- 基本用法
- 事件處理隊列
- 事件訂閱者
- 文件系統與云存儲
- 配置文件
- 基本用法
- 自定義文件系統
- 哈希
- 基本用法
- 輔助方法
- 數組
- 路徑
- 路由
- 字符串
- 網址(URL)
- 其他
- 本地化
- 語言文件
- 基本用法
- 復數
- 驗證
- 覆寫擴展包的語言文件
- 郵件
- 配置
- 基本用法
- 內嵌附件
- 郵件隊列
- 郵件與本地端開發
- 擴展包開發
- 視圖
- 語言
- 配置文件
- 公共資源
- 發布分類文件
- 路由
- 分頁
- 配置
- 使用
- 追加分頁鏈接
- 轉換至 JSON
- 隊列
- 設置
- 基本用法
- 隊列閉包
- 執行一個隊列監聽
- 常駐隊列處理器
- 推送隊列
- 已失敗的工作
- 會話
- 配置
- 使用 Session
- 暫存數據(Flash Data)
- 數據庫 Sessions
- Session 驅動
- 模板
- Blade 模板
- Blade 控制語法結構
- Blade 擴展
- 參考:@section與@yield 介紹
- 單元測試
- 定義并執行測試
- 測試環境
- 從測試調用路由
- 模擬 Facades
- 框架 Assertions
- 輔助方法
- 重置應用程序
- 表單驗證
- 基本用法
- 控制器驗證
- 表單請求驗證
- 使用錯誤信息
- 錯誤信息 & 視圖
- 可用驗證規則
- 條件驗證規則
- 自定義錯誤信息
- 自定義驗證規則
- 數據庫
- 使用基礎
- 配置
- 讀取/寫入連接
- 執行查找
- 數據庫事務處理
- 獲取連接
- 日志記錄
- 查詢構造器
- Selects
- Joins
- 高級 Wheres
- 聚合
- 原生表達式
- 添加
- 更新
- 刪除
- Unions
- 悲觀鎖定 (Pessimistic Locking)
- Eloquent ORM
- 基本用法
- 批量賦值
- 新增,更新,刪除
- 軟刪除
- 時間戳
- 范圍查詢
- Global Scopes
- 關聯
- 關聯查詢
- 預載入
- 新增關聯模型
- 更新上層時間戳
- 使用樞紐表
- 集合
- 獲取器和修改器
- 日期轉換器
- 屬性類型轉換
- 模型事件
- 模型觀察者
- 模型 URL 生成
- 轉換成數組 / JSON
- 結構生成器
- 建立與刪除數據表
- 加入字段
- 修改字段
- 修改字段名稱
- 移除字段
- 檢查是否存在
- 加入索引
- 外鍵
- 移除索引
- 移除時間戳記和軟刪除
- 保存引擎
- 遷移和數據填充
- 建立遷移文件
- 執行遷移
- 回滾遷移
- 數據填充
- Redis
- 配置
- 使用方式
- 管道
- 開發包
- Confide 用戶身份認證
- Entrust 權限管理
- Shoppingcart 購物車
- Genertators 代碼生成工具
- IDE Helper IDE助手
- Artisan 命令行工具
- 概覽
- 用法
- 在命令行接口以外的地方調用命令
- 定時調用 Artisan 命令
- 開發
- 建立自定義命令
- 注冊自定義命令