2

Facade

namespace App\Webshop\Facades;

use Illuminate\Support\Facades\Facade;

class Webshop extends Facade
{
    /**
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return \App\Webshop\Webshop::class;
    }
}

ServiceProvider

namespace App\Webshop;

use Illuminate\Support\ServiceProvider;

class WebshopServiceProvider extends ServiceProvider
{
    /**
     * @return void
     */
    public function register()
    {
        $this->app->bind(\App\Webshop\Webshop::class, function() {
            return new Webshop(new Cart(), new Checkout());
        });

        // or

        $this->app->singleton(\App\Webshop\Webshop::class, function() {
            return new Webshop(new Cart(), new Checkout());
        });
    }
}

Webshop

namespace App\Webshop;

class Webshop
{
    /**
     * @var Cart $cart
     */
    private $cart;

    /**
     * @var Checkout $checkout
     */
    private $checkout;

    public function __construct(Cart $cart, Checkout $checkout)
    {
        $this->cart = $cart;
        $this->checkout = $checkout;
    }

    /**
     * @return Cart
     */
    public function cart()
    {
        return $this->cart;
    }

    /**
     * @return Checkout
     */
    public function checkout()
    {
        return $this->checkout;
    }
}

When i run:

Route::get('test1', function () {
    Webshop::cart()->add(1); // Product id
    Webshop::cart()->add(2); // Product id

    dd(Webshop::cart()->totalPrice());
});

It dumps "24.98" (a price calculation)

But when i run:

Route::get('test2', function () {
    dd(Webshop::cart()->totalPrice());
}); 

It shows me "0"

I think the problem is in the ServiceProvider, because when it registers it creates new objects of Cart and Checkout

How can i fix this issue?

yooouuri
  • 2,578
  • 10
  • 32
  • 54

1 Answers1

-1

Edit : Apart from using a singleton, you're code never persists the cart information to a session or database. PHP does not persist objects after script execution. You need to design your cart to persist the information.

You need a singleton for this. Change $this->app->bind to $this->app->singleton in your service provider. http://www.phptherightway.com/pages/Design-Patterns.html

Laravel does this for your and all you need to do is to bind it as a singleton in the service provider.

Sandeesh
  • 11,486
  • 3
  • 31
  • 42
  • I changed `$this->app->bind` into `$this->app->singleton`, no difference... Do i need to execute some artisan command? – yooouuri May 23 '17 at 12:49
  • Try running `php artisan clear-compiled` and restart if you're using artisan serve. – Sandeesh May 23 '17 at 12:52
  • @Yooouuri wait, share your cart class. Are you not storing the cart information in a session or database? – Sandeesh May 23 '17 at 12:54
  • @Yooouuri ye the issue is also with your cart code. You're storing the cart information in a collection which would perish after the script execution. So when you goto a new route, new collection would be empty. You need to design a better cart implementation. – Sandeesh May 23 '17 at 13:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144931/discussion-between-sandeesh-and-yooouuri). – Sandeesh May 23 '17 at 13:07