Laravel扩展包类 [英] Laravel extend package class
问题描述
我在Laravel安装中添加了购物车包,但是我需要在类中添加一个方法.如果我直接修改该类,当我更新到较新版本时,所做的更改是否会被覆盖?如果是这样,在不中断将来更新的情况下修改软件包的最佳方法是什么?
I added a cart package to my Laravel install, but I need to add a method to the class. If I modify the class directly, will my changes be overwritten when I update to a newer version? If so, what's the best method for modifying a package without breaking future updates?
感谢您的帮助! -JB
Thanks for the help! -JB
推荐答案
我不知道是否有从供应商目录扩展Laravel 5.0软件包的通用过程,并且我确定这对于不同的软件包可能有所不同.但是话说回来,当我想扩展此购物车时,我遇到了同样的问题.但是我以某种方式进行了管理,下面是我遵循的步骤.我希望它能提供一些提示.
I don't know if there is any general process to extend Laravel 5.0 package from vendor directory and I am sure this may be different for different packages. But saying that, I faced the same issue when I wanted to extend this cart . But I managed it somehow and steps I followed are below. I hope it may give some hint.
-
安装软件包
Install the package
composer require "gloudemans/shoppingcart":"~1.3"
创建目录app/Services/Cart
并在其下创建新的类MyCart
Create directory app/Services/Cart
and a new class MyCart
under it
<?php
namespace App\Services\Cart;
use Gloudemans\Shoppingcart\Cart;
class MyCart extends Cart
{
}
在app/Providers
目录下创建CartServiceProvider
,
create CartServiceProvider
under app/Providers
directory,
<?php namespace App\Providers;
use App\Services\Cart\MyCart;
use Illuminate\Support\ServiceProvider;
class CartServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app['mycart'] = $this->app->share(function($app)
{
$session = $app['session'];
$events = $app['events'];
return new MyCart($session, $events);
});
}
}
在app/Services/Cart
目录下创建MyCartFacade
,
<?php
namespace App\Services\Cart;
use Illuminate\Support\Facades\Facade;
class MyCartFacade extends Facade {
protected static function getFacadeAccessor() { return 'mycart'; }
}
在providers
数组中添加以下内容
in config/app.php
add following in providers
array
'App\Providers\CartServiceProvider'
,然后在aliases
数组中
'MyCart' => 'App\Services\Cart\MyCartFacade'
就是这样.现在,在控制器中,我放置了以下代码. add
和content
是基础Cart
类中的方法.
That's it. Now in my controller I placed following code. add
and content
are method in base Cart
class.
\MyCart::add('293ad', 'Product 1', 1, 9.99, array('size' => 'large'));
echo '<pre>';
print_r(\MyCart::content());
exit();
然后是输出
Gloudemans\Shoppingcart\CartCollection Object
(
[items:protected] => Array
(
[0f6524cc3c576d484150599b3682251c] => Gloudemans\Shoppingcart\CartRowCollection Object
(
[associatedModel:protected] =>
[associatedModelNamespace:protected] =>
[items:protected] => Array
(
[rowid] => 0f6524cc3c576d484150599b3682251c
[id] => 293ad
[name] => Product 1
[qty] => 1
[price] => 9.99
[options] => Gloudemans\Shoppingcart\CartRowOptionsCollection Object
(
[items:protected] => Array
(
[size] => large
)
)
[subtotal] => 9.99
)
)
)
)
现在,如果要添加或覆盖功能,只需将该功能放在MyCart
类中即可.
Now if you want to add or override functionality simply put that function in MyCart
class.
好处是,您可以更新基本软件包.
The good thing is, you can update the base package.
我希望它会有所帮助.
I hope it helps.
这篇关于Laravel扩展包类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!