PHP code example of redoy / core-module

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

    

redoy / core-module example snippets



namespace App\Http\Controllers;

use Redoy\CoreModule\Traits\ResponseHelperTrait;
use Redoy\CoreModule\Constants\ApiCodes;

class UserController extends Controller
{
    use ResponseHelperTrait;

    public function index()
    {
        $users = User::all();
        return $this->successResponse($users, ApiCodes::OK, 'Users retrieved successfully');
    }

    public function store(Request $request)
    {
        $user = User::create($request->validated());
        return $this->successResponse($user, ApiCodes::CREATED, 'User created successfully');
    }

    public function destroy($id)
    {
        try {
            User::findOrFail($id)->delete();
            return $this->successResponse(null, ApiCodes::NO_CONTENT, 'User deleted');
        } catch (\Exception $e) {
            return $this->errorResponse(
                ['error' => 'User not found'],
                ApiCodes::NOT_FOUND,
                'Failed to delete user'
            );
        }
    }
}

use Redoy\CoreModule\Constants\ApiCodes;

// 2xx Success codes
ApiCodes::OK;                      // 200
ApiCodes::CREATED;                 // 201
ApiCodes::ACCEPTED;                // 202
ApiCodes::NO_CONTENT;              // 204

// 4xx Client error codes
ApiCodes::BAD_REQUEST;             // 400
ApiCodes::UNAUTHORIZED;            // 401
ApiCodes::FORBIDDEN;               // 403
ApiCodes::NOT_FOUND;               // 404
ApiCodes::UNPROCESSABLE_ENTITY;    // 422

// 5xx Server error codes
ApiCodes::INTERNAL_SERVER_ERROR;   // 500
ApiCodes::SERVICE_UNAVAILABLE;     // 503

// View all codes as array
ApiCodes::HTTP_STATUS_CODES;       // Array of all status codes

return $this->successResponse($data, ApiCodes::OK, 'Operation successful');

return $this->errorResponse($errors, ApiCodes::BAD_REQUEST, 'Validation failed');

return $this->successResponse(null, ApiCodes::NO_CONTENT, 'Deleted');

return $this->successResponse($resource, ApiCodes::CREATED, 'Resource created');
bash
   php artisan package:discover
   
bash
   php artisan vendor:publish --provider="Redoy\CoreModule\Providers\CoreModuleServiceProvider"
   
bash
   php artisan vendor:publish --provider="Redoy\CoreModule\Providers\CoreModuleServiceProvider" --tag="config"
   
bash
   php artisan vendor:publish --provider="Redoy\CoreModule\Providers\CoreModuleServiceProvider" --tag="lang"
   
bash
./vendor/bin/phpunit packages/Redoy/CoreModule/tests/Unit/ResponseHelperTraitTest.php
./vendor/bin/phpunit packages/Redoy/CoreModule/tests/Unit/JsonDrivenResponseHelperTest.php