PHP code example of slexx / laravel-actions

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

    

slexx / laravel-actions example snippets


class UserController 
{
    public function create(CreateUserRequest $request) 
    {
        $user = new User;
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $user->save();
        
        return redirect('users/' . $user->id);
    }
}


class UserApiController 
{
    public function create(CreateUserRequest $request) 
    {
        $user = new User;
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $user->save();
        
        return new UserResponse($user);
    }
}

class UserCreateAction 
{
    public function execute(CreateUserRequest $request): User
    {
        $user = new User;
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $user->save();
        
        return $user;
    }
}

class UserController 
{
    public function create(CreateUserRequest $request, CreateUserAction $action) 
    {
        return redirect('users/' . $action->execute($request)->id);
    }
}


class UserApiController 
{
    public function create(CreateUserRequest $request) 
    {
        return new UserResponse($action->execute($request));
    }
}



namespace App\Actions\Users;

use App\User;
use Slexx\LaravelActions\Action;

class CreateUserAction extends Action
{
    /**
     * @param array $data
     * @return User
     */
    public function execute(array $data)
    {
        $user = User::create($data);

        return $user;
    }
}

php artisan make:action Users/CreateUserAction