1. Go to this page and download the library: Download dersonsena/yii2-tactician 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/ */
class MyClassCommand
{
public $someParam;
public $someOtherParam;
public function __construct($someParam, $someOtherParam = 'defaultValue')
{
$this->someParam = $someParam;
$this->someOtherParam = $someOtherParam;
}
}
class MyClassHandler
{
public function handle(MyClassCommand $command)
{
// do command stuff here!
// we can use $command->someParam and $this->someOtherParam
}
}
public function actionDoSomething()
{
$queryParam = Yii::$app->getRequest()->get('some_param');
// Here the magic happens! =)
$result = Yii::$app->commandBus->handle(new MyClassCommand($queryParam));
if ($result === true) {
return $this->redirect(['go/to/some/place']);
}
return $this->render('some-awesome-view');
}
$config = [
'id' => 'your-app-id',
//...
'container' => [
'definitions' => [
// you can use any string here.
'awesome.alias.to.be.called.anywhere' => MyClassHandler::class
]
],
'components' => [
//...
]
];
class MyClassHandler implements AstrotechLabs\Yii2Tactician\Handler
{
public function handle(MyClassCommand $command)
{
// do command stuff here!
// we can use $command->someParam and $this->someOtherParam
}
public function commandClassName(): string
{
return MyClassCommand::class;
}
}
public function actionDoSomething()
{
$result = Yii::$app->commandBus->handle('awesome.alias.to.be.called.anywhere', [
'someParam' => 'abc',
'someOtherParam' => 'def'
]);
// .. other logics
}