PHP code example of stagerightlabs / actions

1. Go to this page and download the library: Download stagerightlabs/actions 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/ */

    

stagerightlabs / actions example snippets




namespace App\Actions;

use StageRightLabs\Actions\Action;

class MyCoolAction extends Action
{
  public function handle($input = [])
  {
    // business logic goes here...

    return $this->complete('Hooray, it worked!');
  }
}



namespace App\Http\Controllers;

use App\Actions\MyCoolAction;

class Controller
{
  public function post()
  {
    $action = MyCoolAction::execute();

    if ($action->failed()) {
      // send an alert
      return;
    }

    // do something else
    return;
  }
}



namespace App\Actions;

use StageRightLabs\Actions\Action;

class UserCreationAction extends Action
{
  public function handle($input = [])
  {
    // User is created here...

    return $this->complete();
  }

  public function 

public function handle($input = [])
{
  if (is_null($user)) {
    return $this->fail('There was a problem creating your account.');
  }

  return $this->complete('Your new account has been created.');
}

$action = MyCoolAction::execute();

if ($action->failed()) {
  $this->sendAlert($action->getMessage());
  return;
}



namespace App\Actions;

use StageRightLabs\Actions\Action;

class UserCreationAction extends Action
{
  public $user;

  public function handle($input = [])
  {
    $this->user = User::create($input);

    return $this->complete();
  }
}

$action = MyCoolAction::execute();

if ($action->failed()) {
  $this->sendAlert($action->getMessage());
  return;
}

$user = $action->user;

$action = new MyCoolAction(new SomeDependency);
$action = $action->execute();