A Laravel package for generate action classes using Artisan Command.
Requires PHP 8.1+
Require using Composer.
composer require ceceply/action --dev
Put Ceceply\Action\Providers\ActionServiceProvider::class
into list of service providers in config/app.php
.
'providers' => ServiceProvider::defaultProviders()->merge([
/*
* Package Service Providers...
*/
Ceceply\Action\Providers\ActionServiceProvider::class,
/*
* Application Service Providers...
*/
])->toArray(),
In this package, an Action Class is a simple class that has only one task. Example, we have an Action class named CreatePayment
. This class will has only one task, which is to create payments.
Also in this package, by default an Action class will implement interface. That interface will be automatically generated, before the action class generated. Example, if the Action class is named CreatePayment
, the interface that class implements will be named CreatesPayments
. You can customize the interface name later.
Run the following command to generate a new Action Class named CreatePayment
.
php artisan make:action CreatePayment
If that command executed, an Action Class with interface implemented by the class generated.
// app/Actions/Payment/Contracts/CreatesPayments.php
<?php
namespace App\Actions\Payment\Contracts;
use App\Models\Payment;
interface CreatesPayments
{
public function create(array $inputs);
}
// app/Actions/Payment/CreatePayment.php
<?php
namespace App\Actions\Payment;
use App\Actions\Payment\Contracts\CreatesPayments;
use App\Models\Payment;
class CreatePayment implements CreatesPayments
{
public function create(array $inputs)
{
// TODO: create
}
}
You can customize the method name when writing the command.
If the method name is
create
orupdate
, method will automatically have an array parameter namedinputs
.
php artisan make:action Payment/CreatePayment --action=handle
Or shorter.
php artisan make:action Payment/CreatePayment -ahandle
Output.
// app/Actions/Payment/Contracts/CreatesPayments.php
interface CreatesPayments
{
public function handle();
}
// app/Actions/Payment/CreatePayment.php
class CreatePayment implements CreatesPayments
{
public function handle()
{
// TODO: handle
}
}
You can add a model to your Action Class. The model will be placed in first parameter of the action method.
php artisan make:action Payment/CreatePayment --model=Payment
Or shorter.
php artisan make:action Payment/CreatePayment -mPayment
Output.
// app/Actions/Payment/Contracts/CreatesPayments.php
<?php
namespace App\Actions\Payment\Contracts;
use App\Models\Payment;
interface CreatesPayments
{
public function create(Payment $payment, array $inputs);
}
// app/Actions/Payment/CreatePayment.php
<?php
namespace App\Actions\Payment;
use App\Actions\Payment\Contracts\CreatesPayments;
use App\Models\Payment;
class CreatePayment implements CreatesPayments
{
public function create(Payment $payment, array $inputs)
{
// TODO: create
}
}
Instead of defining model manually, you can add --guess-model
option when writing the command.
php artisan make:action Payment/CreatePayment --guess-model
Or shorter.
php artisan make:action Payment/CreatePayment -g
Last word of the class name will be considered as the model name. If the last word is plural, it will be changed to singular first.
You can customize interface name when writing the command.
php artisan make:action Payment/CreatePayment --interface=Payment/Contracts/CreatePaymentContracts
Or shorter.
php artisan make:action Payment/CreatePayment -iPayment/Contracts/CreatePaymentContracts
You can create an Action Class without implementing an interface.
php artisan make:action Payment/CreatePayment --without-interface
Or shorter.
php artisan make:action Payment/CreatePayment -w
Create the class even if the class already exists.
php artisan make:action Payment/CreatePayment --force
Or shorter.
php artisan make:action Payment/CreatePayment -f
Create the interface and the class even if the class and the interface already exists.
php artisan make:action Payment/CreatePayment --force-both
Or shorter.
php artisan make:action Payment/CreatePayment -F
If you want to create the interface only, you can do that by using the make:iaction
command. So only the interface generated.
php artisan make:iaction Payment/CreatesPayments
Even if you create the interface only, you can still customize action method name, add model, guess model and force create interface.
php artisan make:iaction Payment/CreatesPayments -ahandle
php artisan make:iaction Payment/CreatesPayments -mPayment
php artisan make:iaction Payment/CreatesPayments -g
php artisan make:iaction Payment/CreatesPayments -f
This package is an open-sourced software licensed under the MIT license.