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());
});
}
}
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?
via Yooouuri