[英]Extending Vendor Class in Laravel
I am using a cart system (\\Gloudemans\\Shoppingcart) but I want to override the default total() method: 我正在使用购物车系统(\\ Gloudemans \\ Shoppingcart),但我想覆盖默认的total()方法:
namespace Gloudemans\Shoppingcart;
/**
* Get the total price of the items in the cart.
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
* @return string
*/
public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();
$total = $content->reduce(function ($total, CartItem $cartItem) {
return $total + ($cartItem->qty * $cartItem->priceTax);
}, 0);
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
}
My extension: 我的扩展名:
namespace App\Http\Controllers;
use Gloudemans\Shoppingcart\Facades\Cart;
use \Illuminate\Http\Request;
class CartController extends Cart
{
function __construct() {
}
public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null) {
$content = $this->getContent();
$total = $content->reduce(function ($total, CartItem $cartItem) {
return $total + ($cartItem->qty * $cartItem->priceTax);
}, 0);
$currency = new Currency();
$currency_id = $currency->where('code', session()->get('currency'))->first()->id;
$rate = CurrencyRate::where('currency_id', $currency_id)->latest()->first()->rate;
return $this->numberFormat($total*$rate, $decimals, $decimalPoint, $thousandSeperator);
}
}
However I am not sure how to access it, this is how I access the default method which works: 但是我不知道如何访问它,这是我访问默认方法的方法:
...
<span id="cart_total" class="btn btn-default"><i class="fa fa-shopping-cart" aria-hidden="true"></i>
{{$symbol}} {{\Gloudemans\Shoppingcart\Facades\Cart::total()}}</span>
...
The facade above is: 上面的立面是:
namespace Gloudemans\Shoppingcart\Facades;
use Illuminate\Support\Facades\Facade;
class Cart extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'cart';
}
}
Obviously the following doesn't work: 显然以下不起作用:
{{\App\Http\Controllers\CartController::total()}}
Here is a link to the cart system I am using: 这是我正在使用的购物车系统的链接:
https://github.com/Crinsane/LaravelShoppingcart https://github.com/Crinsane/LaravelShoppingcart
You have to have a dedicated class that overrides the default functionality. 您必须拥有一个覆盖默认功能的专用类。
class myCart extends Cart {
public function total($decimals = null ....
}
now if you can create new instance of the myCart and use it to calculate total 现在,如果你可以创建myCart的新实例并使用它来计算总数
var obj = new myCart();
obj->total();
If needed you can create the fascade for myCart class and use it. 如果需要,您可以为myCart类创建fascade并使用它。
Doing the following in a facade: 在立面中执行以下操作:
Facade::something();
is a shortcut for 是一个快捷方式
app()->make(Facade::getFacadeAccessor())->something();
You therefore need to extend class returned by making a "cart" and override the total
method in there. 因此,您需要通过制作“购物车”来扩展返回的类,并覆盖其中的total
方法。
Eg assume : 假设:
class Cart {
//...
public function total() {}
}
class MyCart extends Cart {
//...
//Your total:
public function total() {}
}
Note: The first cart here is not the facade but the actual class that the facade returns. 注意:这里的第一个购物车不是外观,而是外观返回的实际类。
Then you need to rebind it. 然后你需要重新绑定它。 To do this you need to make sure you have a service provider that runs after it's bound the first time and then: 要做到这一点,你需要确保你有一个服务提供程序,它在第一次绑定后运行,然后:
public function handle() {
$this->app->bind("cart", function ($app) { return new MyCart(); });
}
There shouldn't be any need to override the facade. 不应该需要覆盖立面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.