Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Response Maker Bundle Laravel Package

deozza/response-maker-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation

    composer require deozza/response-maker-bundle
    

    Ensure the package is registered in config/app.php under providers:

    Deozza\ResponseMakerBundle\ResponseMakerServiceProvider::class,
    
  2. Basic Usage Inject the ResponseMaker service into your controller or service:

    use Deozza\ResponseMakerBundle\ResponseMaker;
    
    class ExampleController extends Controller
    {
        protected $responseMaker;
    
        public function __construct(ResponseMaker $responseMaker)
        {
            $this->responseMaker = $responseMaker;
        }
    
        public function index()
        {
            $data = ['message' => 'Hello, World!'];
            return $this->responseMaker->make($data);
        }
    }
    
  3. First Use Case Replace manual JSON responses with a standardized approach:

    // Before
    return response()->json(['success' => true]);
    
    // After
    return $this->responseMaker->make(['success' => true]);
    

Implementation Patterns

Standardized Response Workflows

  • Consistent JSON Responses Use make() for all API responses to enforce a uniform structure:

    $response = $this->responseMaker->make([
        'data' => $user,
        'meta' => ['status' => 'success']
    ]);
    
  • HTTP Status Codes Pass a status code as the second argument:

    return $this->responseMaker->make(['error' => 'Not Found'], 404);
    
  • Error Handling Standardize error responses:

    try {
        $data = $this->fetchData();
    } catch (\Exception $e) {
        return $this->responseMaker->make(['error' => $e->getMessage()], 500);
    }
    

Integration with Laravel Features

  • API Resources Combine with Laravel’s ApiResource for structured payloads:

    return $this->responseMaker->make(new UserResource($user));
    
  • Form Requests Validate and respond using ResponseMaker:

    public function store(StoreUserRequest $request)
    {
        $validated = $request->validated();
        return $this->responseMaker->make($validated);
    }
    
  • Middleware Responses Use in middleware for unauthorized access:

    return $this->responseMaker->make(['error' => 'Unauthorized'], 401);
    

Advanced Patterns

  • Custom Response Classes Extend the ResponseMaker to add domain-specific logic:

    class ApiResponseMaker extends ResponseMaker
    {
        public function success($data, $message = 'Success')
        {
            return $this->make([
                'success' => true,
                'message' => $message,
                'data' => $data
            ]);
        }
    }
    
  • Response Wrapping Automatically wrap responses in a meta layer:

    $this->responseMaker->wrap($data, ['status' => 'ok']);
    

Gotchas and Tips

Pitfalls

  • No Built-in Validation The package does not validate input data. Ensure data is sanitized before passing to make():

    // Avoid:
    $this->responseMaker->make($_GET); // Unsafe!
    
    // Do:
    $this->responseMaker->make(array_filter($_GET));
    
  • Overriding Default Behavior If extending ResponseMaker, ensure parent methods are called:

    public function make($data, $status = 200, array $headers = [])
    {
        $data = $this->customTransform($data); // Add logic here
        return parent::make($data, $status, $headers);
    }
    
  • Performance with Large Data Avoid passing large arrays/objects directly. Use json_encode options or chunk data:

    $this->responseMaker->make($data, 200, [], JSON_PRETTY_PRINT);
    

Debugging

  • Check Headers If responses lack Content-Type: application/json, verify no middleware is overriding headers:

    $this->responseMaker->make($data, 200, ['Content-Type' => 'application/json']);
    
  • Logging Responses Log responses for debugging:

    \Log::debug('Response:', [
        'data' => $data,
        'status' => $status,
        'headers' => $headers
    ]);
    

Extension Points

  • Custom Serializers Override the serialize() method to support custom data types (e.g., DateTime):

    public function serialize($data)
    {
        if ($data instanceof \DateTime) {
            return $data->format('Y-m-d H:i:s');
        }
        return parent::serialize($data);
    }
    
  • Response Hooks Add hooks for pre/post-processing:

    $this->responseMaker->onMake(function ($data, $status) {
        $data['timestamp'] = now()->toIso8601String();
        return $data;
    });
    
  • Testing Mock ResponseMaker in tests:

    $mock = Mockery::mock(ResponseMaker::class);
    $mock->shouldReceive('make')->andReturn(response()->json(['mocked' => true]));
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui