Laravel extend package class

前端 未结 1 728
梦毁少年i
梦毁少年i 2021-02-06 16:53

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 v

相关标签:
1条回答
  • 2021-02-06 17:37

    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.

    1. Install the package

      composer require "gloudemans/shoppingcart":"~1.3"
      
    2. 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
      {
      
      }
      
    3. 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);
          });
      }
      

      }

    4. Create MyCartFacade under app/Services/Cart directory,

      <?php
      
      namespace App\Services\Cart;
      
      use Illuminate\Support\Facades\Facade;
      
      class MyCartFacade extends Facade {
          protected static function getFacadeAccessor() { return 'mycart'; }
      }
      
    5. in config/app.php add following in providers array

      'App\Providers\CartServiceProvider'
      

      and following in aliases array

      'MyCart'      => 'App\Services\Cart\MyCartFacade'
      

      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();
      

      and following is the output,

      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
                              )
      
                      )
      
              )
      
      )
      

    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.

    0 讨论(0)
提交回复
热议问题