Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Extending the functionality of the Laravel HTTP client

License

NotificationsYou must be signed in to change notification settings

TheDragonCode/laravel-http-macros

Repository files navigation

the dragon code laravel http macros

Stable VersionTotal DownloadsGithub Workflow StatusLicense

Installation

To get the latest version ofHTTP Macros, simply require the project usingComposer:

composer require dragon-code/laravel-http-macros

Configuration

If desired, you can publish the configuration file using the console command:

php artisan vendor:publish --provider="DragonCode\\LaravelHttpMacros\\ServiceProvider"

If your application already has aconfig/http.php file, then you can simply add a newmacros key from theconfiguration file to it.

Here you can specify a list of your classes for registering macros.Macro classes must inherit from the abstract classDragonCode\LaravelHttpMacros\Macros\Macro.

You can also redefine macro names using an associative array. For example:

// Configreturn ['macros' => ['request' => [            WithLoggerMacro::class,        ],'response' => [            ToDataMacro::class,        ],    ],];// MacroHttp::withLogger('some')->get();Http::withLogger('some')->get()->toData(...);Http::get()->toData(...);
// Configreturn ['macros' => ['request' => ['qwerty' => WithLoggerMacro::class,        ],'response' => ['qwerty' => ToDataMacro::class,        ],    ],];// MacroHttp::qwerty('some')->get();Http::qwerty('some')->get()->qwerty(...);Http::qwerty('some')->get()->toData(...);// method not foundHttp::get()->qwerty(...);Http::get()->toData(...);// method not found

Usage

Available Methods

Request

Response

Method Listing

withLogger()

Adds the ability to log HTTP requests and responses.

useIlluminate\Support\Facades\Http;Http::withLogger('some_channel')->get();

This method will log HTTP requests and responses.

It is also possible to use your own handler, message formatting and path to the log file.To do this, you need to specify the desired channel name from the log file and define the necessary parameters in it.

For example:

// config/logging.phpreturn [// ...'channels' => ['some' => ['driver' =>'single','level' =>env('LOG_LEVEL','debug'),'path' =>storage_path('logs/some.log'),'handler' => \App\Logging\SomeHandlerStack::class,'formatter' => \App\Logging\MessageFormatter::class,        ],    ],];// Usagereturn Http::withLogger('some')->...

toData()

The class instance will be returned.

useIlluminate\Support\Facades\Http;// Returns a SomeData objectreturn Http::get()->toData(SomeData::class);// Will return a SomeData object generated from the JSON pathreturn Http::get()->toData(SomeData::class,'data.item');// Returns the result of the callback executionreturn Http::get()->toData(fn (array$data) =>newSomeData($data['data']['item']['id'],$data['data']['item']['title']    ));// Returns the result of the callback execution from a custom JSON pathreturn Http::get()->toData(fn (array$data) =>newSomeData($data['id'],$data['title']),'data.item');

Note

If afrom method exists in a class, then it will be called to construct the object.

Compatible withSpatie Laravel Data.

class SomeData{publicfunction__construct(publicint$id,publicstring$title    ) {}publicstaticfunctionfrom(array$data):static    {returnnewstatic(...$data);    }}return Http::get()->toData(SomeData::class);

toDataCollection()

TheIlluminate\Support\Collection object or an object inherited from it will be returned.

useIlluminate\Support\Facades\Http;// Returns a collection of SomeData objectsreturn Http::get()->toDataCollection(SomeData::class);// Returns a collection of SomeData objects formed from the JSON pathreturn Http::get()->toDataCollection(SomeData::class,'data.item');// Returns the result of the callback executionreturn Http::get()->toDataCollection(fn (array$data) =>collect([newSomeData($data['data']['item']['id'],$data['data']['item']['title']        ),    ]));// Returns the result of the callback execution from a custom JSON pathreturn Http::get()->toDataCollection(fn (array$data) =>collect([newSomeData(...$data),    ]),'data.item');

Note

If acollect method exists in a class, then it will be called to construct the collection.

Compatible withSpatie Laravel Data.

useIlluminate\Support\Collection;class SomeData{publicfunction__construct(publicint$id,publicstring$title    ) {}publicstaticfunctioncollect(array$items):Collection    {returncollect($items)->map(fn (array$item) =>newstatic(...$item)        );    }}return Http::get()->toDataCollection(SomeData::class);

Generate IDE Helper files

You can generate helper files for the IDE using the console command:

php artisan http:macros-helper

This will help your IDE suggest methods.

IDE Helper

License

This package is licensed under theMIT License.

About

Extending the functionality of the Laravel HTTP client

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Sponsor this project

    Contributors4

    •  
    •  
    •  
    •  

    Languages


    [8]ページ先頭

    ©2009-2025 Movatter.jp