PHP code example of myerscode / laravel-sub-request

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

    

myerscode / laravel-sub-request example snippets


use Myerscode\Laravel\SubRequest\Dispatcher;
use Myerscode\Laravel\SubRequest\SubRequest;

class MyController
{
    public function __construct(private readonly Dispatcher $subRequest) {}

    // Using dependency injection
    public function withInjection()
    {
        return $this->subRequest->post('/auth', ['foo' => 'bar']);
    }

    // Using the facade
    public function withFacade()
    {
        return SubRequest::dispatch('GET', '/details', ['foo' => 'bar']);
    }

    // Using the helper
    public function withHelper()
    {
        return subrequest('GET', '/details', ['foo' => 'bar']);
    }
}

$dispatcher->get('/url', $data);
$dispatcher->post('/url', $data);
$dispatcher->put('/url', $data);
$dispatcher->patch('/url', $data);
$dispatcher->delete('/url', $data);
$dispatcher->options('/url', $data);

// Set Authorization and Accept headers
$dispatcher->get('/api/users', [], [
    'Authorization' => 'Bearer my-token',
    'Accept' => 'application/json',
]);

// Works with the facade and helper too
SubRequest::dispatch('GET', '/api/users', [], ['Authorization' => 'Bearer my-token']);
subrequest('GET', '/api/users', [], ['Authorization' => 'Bearer my-token']);

// Set cookies on the sub request
$dispatcher->get('/api/profile', [], [], [
    'session_id' => 'abc123',
    'token' => 'my-auth-token',
]);

// Combine headers and cookies
$dispatcher->post('/api/data', ['key' => 'value'], [
    'Accept' => 'application/json',
], [
    'session_id' => 'abc123',
]);

// Works with the facade and helper too
SubRequest::dispatch('GET', '/api/profile', [], [], ['session_id' => 'abc123']);
subrequest('GET', '/api/profile', [], [], ['session_id' => 'abc123']);

use App\Http\Middleware\Authenticate;
use App\Http\Middleware\RateLimiter;

// Skip a single middleware
$dispatcher->withoutMiddleware(Authenticate::class)->get('/api/internal');

// Skip multiple middleware by chaining
$dispatcher
    ->withoutMiddleware(Authenticate::class)
    ->withoutMiddleware(RateLimiter::class)
    ->post('/api/internal', $data);

// Or pass an array
$dispatcher->withoutMiddleware([Authenticate::class, RateLimiter::class])->get('/api/internal');