PHP code example of eryw / pastry-bag

1. Go to this page and download the library: Download eryw/pastry-bag library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eryw / pastry-bag example snippets


Plugin::load('PastryBag', ['bootstrap' => true]);

DispatcherFactory::add('ControllerFactory');

DispatcherFactory::add('PastryBag\Routing\Filter\ControllerFactoryFilter');

class Application extends BaseApplication
{
    // ... //
    
    protected function getDispatcher()
    {
        return new ActionDispatcher(new ControllerFactory(), null, DispatcherFactory::filters());
    }
}

class AppController extends \PastryBag\Controller\Controller
{
    // ... //
}

class UsersController extends AppController
{
    protected $payment;

    public function __construct(PaymentService $payment)
    {
        parent::__construct();
        $this->payment = $payment;
    }
    
    public function payBill()
    {
        // `$this->payment` will auto injected with instance of PaymentService
        $this->payment->anyMethodOfPaymentService();
    }
}

class RemoteGaleryController extends AppController
{
    public function index($id, MyHttpClient $client)
    {
        // `$client` will auto injected with instance of MyHttpClient
        $client->request('GET', 'http://remotesite.com');
    }
}

use Aura\Di\Container;
use Aura\Di\ContainerConfig;
use Cake\ORM\TableRegistry;

// OPTIONAL. You can move this class to other file if you want
class DiConfig extends ContainerConfig
{
    public function define(Container $di)
    {
        $di->set(\App\Model\Table\UsersTable::class, $di->lazy(function () {
            return TableRegistry::get('Users');
        }));
        $di->types[\App\Model\Table\UsersTable::class] = $di->lazyGet(\App\Model\Table\UsersTable::class);
    }
}

// REQUIRED. This file must return list of configs as array
return [
    'My\Awesome\App\ClassImplementsAuraDiContainerConfigInterface',
    new DiConfig,
];

// `$di` is instance of Aura\Di\Container
$di = PastryBag::getContainer();