PHP code example of smmehdisharifi / laravel-msgpack

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

    

smmehdisharifi / laravel-msgpack example snippets


use Illuminate\Support\Facades\Route;

Route::middleware('msgpack')->get('/api/profile', function () {
    return [
        'name' => 'Laravel',
        'format' => 'negotiated',
    ];
});

use Illuminate\Http\Request;

Route::middleware('msgpack')->post('/api/profile', function (Request $request) {
    return [
        'received' => $request->msgpack(),
        'name' => $request->input('name'),
    ];
});

return response()->msgpack([
    'message' => 'Created',
], 201, [
    'X-Request-Id' => $requestId,
]);

response()->msgpack($value, $status = 200, $headers = []);

use App\Http\Resources\UserResource;
use App\Models\User;
use Illuminate\Support\Facades\Route;

Route::middleware('msgpack')->get('/api/users', function () {
    return UserResource::collection(
        User::query()->paginate(25),
    );
});

return response()->msgpack(new UserResource($user));

use Msgpack;

$data = ['name' => 'Laravel', 'type' => 'framework'];

$packed = Msgpack::encode($data);
$unpacked = Msgpack::decode($packed);

return [
    'content_type' => 'application/msgpack',

    'request_content_types' => [
        'application/msgpack',
        'application/x-msgpack',
    ],

    'accept_content_types' => [
        'application/msgpack',
        'application/x-msgpack',
    ],

    'max_request_size' => 10 * 1024 * 1024,

    'max_depth' => 64,

    'max_nodes' => 100000,
];

protected $middlewareAliases = [
    'msgpack' => \SmMehdiSharifi\LaravelMsgpack\Middleware\MsgpackMiddleware::class,
];
bash
php artisan msgpack:benchmark
bash
php artisan msgpack:benchmark --iterations=5000
php artisan msgpack:benchmark --iterations=5000 --json
bash
php artisan vendor:publish --tag=msgpack-config