Good day,
I’m merging to slim projects using the League Container. The problem, can’t access the content of cart”Basket” session inside twig. Most of my classes implements the SessionInterface and the basket implements BucketInterface. Think it’s a mix-up of the implementation of the interfaces. the SessionInterface have access to twig
Bucket Cart files
Basket.php
BucketStorage.php
BucketInterface.php
BasketServiceProvider.php
BucketInterfaceServiceProvider.php
Twig Session Files
Session.php
SessionInterface.php
SessionServiceProvider.php
ViewServiceProvider.php
ViewShareServiceProvider.php
This is a dump generated by the BucketStorage cart session
array:2 [▼
"_token" => "5ec364b01106a7584dd16701c7167117f3f6c6c41b4353a39dcf49f0b7316a53"
"cart" => array:2 [▼
33 => array:2 [▼
"product_id" => 33
"quantity" => 3
]
13 => array:2 [▼
"product_id" => 13
"quantity" => 2
Dump from within Twig, need to access Cart Session detail. Cart missing…
array:8 [▼
"app_name" => "Ti"
"config" => Config {#16 ▶}
"auth" => Auth {#101 ▶}
"csrf" => Csrf {#115 ▶}
"flash" => Flash {#118 ▶}
"menu" => MenuDsp {#120}
"errors" => []
"old" => []
]
File : BucketStorage.php
namespace App\Session;
use Countable;
use App\Session\Contracts\BucketInterface;
class BucketStorage implements BucketInterface, Countable {
protected $bucket;
public function __construct($bucket = 'default') {
if (!isset($_SESSION[$bucket])) {
$_SESSION[$bucket] = [];
}
$this->bucket = $bucket;
}
… more detail….
}
High level File Structure
App\Basket\Basket.php
class Basket {
public function __construct(BucketInterface $storage, Product $product)
// more code
}
App\Session\Session.php
class Session implements SessionInterface
public function __construct(BucketInterface $storage, Product $product)
// more code
}
App\Session\BucketStorage.php
class BucketStorage implements BucketInterface, Countable {
public function __construct(BucketInterface $storage, Product $product)
// more code
}
Providers
File: ViewShareServiceProvider.php
namespace App\Providers;
use App\Auth\Auth;
use App\Menu\MenuDsp;
use App\Security\Csrf;
use App\Session\Flash;
use App\Views\View;
use League\Container\ServiceProvider\AbstractServiceProvider;
use League\Container\ServiceProvider\BootableServiceProviderInterface;
class ViewShareServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface
{
public function boot()
{
$container = $this->getContainer();
$container->get(View::class)->share([
'config' => $container->get('config'),
'auth' => $container->get(Auth::class),
'csrf' => $container->get(Csrf::class),
'flash' => $container->get(Flash::class),
'menu' => $container->get(MenuDsp::class),
// I tried to add the session card, detail here.
//
]);
}
public function register()
{
//
}
}
File :BasketServiceProvider.php
namespace App\Providers;
use App\Basket\Basket;
use App\Session\Contracts\BucketInterface;
use App\Models\Product;
use League\Container\ServiceProvider\AbstractServiceProvider;
use App\Session\Contracts\SessionInterface;
use App\Views\View as Twig;
//Basket::class => function (ContainerInterface $c) {
// return new Basket($c->get(BucketInterface::class), $c->get(Product::class));
class BasketServiceProvider extends AbstractServiceProvider {
protected $provides = [
Basket::class,
BucketInterface::class
//Product::class
];
public function register() {
$container = $this->getContainer();
$container->share(Basket::class, function () use ($container) {
return new Basket(
$container->get(BucketInterface::class),
$container->get(Product::class),
$container->get(SessionInterface::class)
);
});
}
}
**File : BucketInterfaceServiceProvider.php **
namespace App\Providers;
use App\Session\Contracts\BucketInterface;
use App\Session\BucketStorage;
use League\Container\ServiceProvider\AbstractServiceProvider;
//use League\Container\ServiceProvider\BootableServiceProviderInterface;
use App\Views\View as Twig;
//BucketInterface::class => function (ContainerInterface $c) {
// return new BucketStorage('cart');
class BucketInterfaceServiceProvider extends AbstractServiceProvider {
protected $provides = [
BucketInterface::class,
];
public function register() {
$container = $this->getContainer();
$container->share(BucketInterface::class, function () use ($container) {
return new BucketStorage('cart');
});
}
}
**File : ViewServiceProvider.php **
<?php
namespace App\Providers;
use App\Views\Extensions\DebugExtension;
use App\Views\Extensions\RouteExtension;
use App\Views\View;
use League\Container\ServiceProvider\AbstractServiceProvider;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class ViewServiceProvider extends AbstractServiceProvider
{
protected $provides = [
View::class
];
public function register()
{
$container = $this->getContainer();
$config = $container->get('config');
$container->share(View::class, function () use ($config, $container) {
$loader = new FilesystemLoader(base_path('resources/views'));
$twig = new Environment($loader, [
'cache' => $config->get('twig.cache'),
'debug' => $config->get('twig.debug')
]);
$twig->addExtension(new RouteExtension(
$container->get('router'),
$container->get('request')->getUri()
));
$twig->addExtension(new DebugExtension);
foreach ($config->get('twig.globals') as $key => $value) {
$twig->addGlobal($key, $value);
}
return new View($twig);
});
}
}