Laravel package for interactions.
Make sure all dependencies have been installed before moving on:
Pull the package via Composer:
$ composer require nasyrov/laravel-interactions
Register the service provider in config/app.php
:
'providers' => [
...
Nasyrov\Laravel\Interactions\InteractionServiceProvider::class,
...
]
Let's generate a typical user registration flow with the use of interactions.
First, generate a RegisterUser
interaction via the command:
$ php artisan make:interaction RegisterUser
The command above will create a new file app/Interactions/RegisterUser.php
.
Let's open the file and tailor it for our needs – create a new user:
use App\User;
use Illuminate\Http\Request;
use Nasyrov\Laravel\Interactions\Contracts\Interaction;
class RegisterUser implements Interaction
{
/**
* Handle the interaction.
*
* @return mixed
*/
public function handle(Request $request)
{
return User::create([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password),
]);
}
}
Next, include the CallsInteractions
trait into the base controller so we will be able to run the interactions in any other controller that extends the one:
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Nasyrov\Laravel\Interactions\CallsInteractions;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, CallsInteractions, ValidatesRequests;
}
Finally, in the UsersController
controller run the RegisterUser
interaction and pass the request object:
use App\Http\Controllers\Controller;
use App\Interaction\RegisterUser;
use Illuminate\Http\Request;
class UsersController extends Controller
{
public function register(RegisterUserRequest $request)
{
return $this->interact(RegisterUser::class, [$request]);
}
}
$ composer lint
$ composer test
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.