1. Go to this page and download the library: Download therour/laravel-actionable 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/ */
Route::action('GET', '/users/{id}', App\User\Actions\GetUser::class);
// If you need to define your FormRequest class
Route::action('POST', '/users', App\user\Actions\CreateUser::class);
use Therour\Actionable\Contracts\Actionable;
use App\User\Models\User;
class GetUser implements Actionable
{
/**
* @var mixed
*/
protected $result;
/**
* @var \App\User\Models\User
*/
protected $model;
/**
* Initiate a single action class
*/
public function __construct(User $model)
{
$this->model = $model;
}
/**
* Start running the action
*
* @var array $data
* @return mixed
*/
public function run($id)
{
return $this->result = $this->model->find($id);
}
}
use Therour\Actionable\Params\AbstractParam;
class CreateUserParam extends AbstractParam
{
private $name;
private $email;
public function getName()
{
return $this->name;
}
public function getEmail()
{
return strtolower($this->email);
}
public static function rules()
{
return [
'name' => '
...
public function run(CreateUserParam $param)
{
// load all filtered parameters. with `getParameters()`
// $this->model->create($param->getParameters())
$this->model->create([
'user' => $param->getName(),
'email' => $param->getEmail()
]);
}
...
public function blabla(Request $request, CreateUser $action, CreateUserParam $param)
{
$param->create($request->all())
->validate();
$user = $action->run($param);
return $user;
}
...
use Illuminate\Http\JsonResponse;
class GetUser implements Actionable, Responsable
{
...
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function toResponse($request)
{
// accessing the result of `run` method by assigned `result` attribute
return new JsonResponse(['data' => $this->result]);
}
}