This week, the Laravel team released v11.35, which includes URI parsing and mutation, the ability to disable HTTP client exception response truncation, transforming an HTTP response to a Fluent instance, and more.
URI Parsing and Mutation
Taylor Otwell contributed a Uri
class for URI parsing and mutation.
$uri = Uri::of('https://laravel.com')
->withQuery(['name' => 'Taylor'])
->withPath('/docs/installation')
->withFragment('hello-world');
$uri->scheme();
$uri->host();
$uri->user();
$uri->password();
$uri->path();
$uri->port();
$uri->query()->all();
$uri->query()->has('name');
$uri->query()->missing('name');
$uri->query()->decode();
// And more...
return (string) $uri;
You can also retrieve a URI instance from the current request, named routes, and URLs:
$uri = $request->uri();
$uri = Uri::to('/something')->withQuery(...);
$uri = Uri::route('named-route', ['user' => $user]);
See Pull Request #53731 for more details.
Customize or Disable HTTP Client Exception Message Truncation
Steve Bauman contributed the ability to customize or disable HTTP client response truncation via the application bootstrap configuration. In the withExceptions()
callback, you can customize the truncation length or disable fully disable it with the following:
// bootstrap/app.php
use IlluminateHttpClientRequestException;
return Application::configure(basePath: dirname(__DIR__))
// ...
->withExceptions(function (Exceptions $exceptions) {
$exceptions->dontTruncateRequestExceptions();
// Or ...
$exceptions->truncateRequestExceptionsAt(260);
})->create();
Transform HTTP Client Response Data into a Fluent Instance
Steve Bauman contributed the ability to transform an HTTP Client response data into a fluent instance:
$fluent = Http::get('https://api.example.com/products/1')->fluent();
$fluent->float('price'); // 420.69
$fluent->collect('colors'); // Collection(['red', 'blue'])
$fluent->boolean('on_sale'); // true
$fluent->integer('current_stock'); // 69
Add The Conditionable
Trait to Request
Ahmet Imamoglu added the Conditionable
trait the the HTTP request. Just like the query builder, you can use when()
in a request class:
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
$this->when($this->input('account_id'),
fn (Request $req, int $accountId) => $req->merge(['account_name' => Account::getName($accountId)]),
fn (Request $req) => $req->merge(['account_name' => null])
)->when($this->input('contact_id'),
fn (Request $req, int $contactId) => $req->merge(['contact_name' => Contact::getName($contactId)]),
fn (Request $req) => $req->merge(['contact_name' => null])
);
}
Allow Sorting Routes by Definition in route:list
Mathieu TUDISCO contributed the ability to sort route:list
results by definition
:
php artisan route:list --sort=definition
Here’s how it works by default and then using the definition value:
Schedule Handling: Ping on Success and Failure
Luca Castelnuovo contributed pingOnSuccessIf()
and pingOnFailureIf()
to schedule handling. This allows the scheduler to ping a URL when a task fails or succeeds:
$pingBackupRun = (bool) config('services.ohdear.tasks.backup:run', false);
$pingBackupRunUrl = config('services.ohdear.tasks.backup:run');
Schedule::command('backup:run')->hourly()
->pingBeforeIf($pingBackupRun, $pingBackupRunUrl . '/starting')
->pingOnSuccessIf($pingBackupRun, $pingBackupRunUrl . '/finished')
->pingOnFailureIf($pingBackupRun, $pingBackupRunUrl . '/failed');
See Pinging URLs in the Laravel documentation.
Release notes
You can see the complete list of new features and updates below and the diff between 11.34.0 and 11.35.0 on GitHub. The following release notes are directly from the changelog:
v11.35.0
- [11.x] Supports Symfony 7.2 by @crynobone in https://github.com/laravel/framework/pull/53585
- [11.x] Fix database reconnecting logic by @stancl in https://github.com/laravel/framework/pull/53693
- [11.x] Test Improvements by @crynobone in https://github.com/laravel/framework/pull/53708
- [11.x] Fix foreignIdFor() when the foreign key is a non-incrementing integer other than ULID by @edgrosvenor in https://github.com/laravel/framework/pull/53696
- [11.x] Allow sorting routes by precedence in artisan routes:list. by @mathieutu in https://github.com/laravel/framework/pull/53706
- [11.x] Update the message for the schedule:work command. by @AbdelElrafa in https://github.com/laravel/framework/pull/53710
- [11.x] Support auto-discovery of PSR-17 implementations by @hafezdivandari in https://github.com/laravel/framework/pull/53711
- [11.x] Improve Error Handler in the ProcessDriver by @WillTorres10 in https://github.com/laravel/framework/pull/53712
- [11.x] Comment grammar fixes by @nexxai in https://github.com/laravel/framework/pull/53714
- [11.x] Replace get_called_class with static::class by @fernandokbs in https://github.com/laravel/framework/pull/53725
- [11.x] Add the pivot’s related model when creating from attributes by @alexwass-lr in https://github.com/laravel/framework/pull/53694
- [11.x] use a consistent alias for
IlluminateDatabaseEloquentCollection
by @browner12 in https://github.com/laravel/framework/pull/53730 - [11.x] switch
Collection::make()
fornew Collection()
by @browner12 in https://github.com/laravel/framework/pull/53733 - [11.x] always alias the
IlluminateDatabaseEloquentCollection
by @browner12 in https://github.com/laravel/framework/pull/53735 - [11.x] convert
collect()
helper tonew Collection()
by @browner12 in https://github.com/laravel/framework/pull/53726 - [11.x] Improves
Collection
support for enums usingfirstWhere()
andvalue()
by @crynobone in https://github.com/laravel/framework/pull/53777 - [11.x] Add Conditionable Trait to Request by @ahmeti in https://github.com/laravel/framework/pull/53775
- [11.x] Ignore health endpoint when in maintenance mode by @joshmanders in https://github.com/laravel/framework/pull/53772
- [11.x] Add ability to transform
HttpClientResponse
intoFluent
by @stevebauman in https://github.com/laravel/framework/pull/53771 - set schema to smtps if MAIL_ENCRYPTION === tls by @danielrona in https://github.com/laravel/framework/pull/53749
- [11.x] more consistent and readable chaining by @browner12 in https://github.com/laravel/framework/pull/53748
- [11.x] Fix the RateLimiter issue when using dynamic keys by @MilesChou in https://github.com/laravel/framework/pull/53763
- [11.x] Add ability to customize or disable
HttpClientRequestException
message truncation by @stevebauman in https://github.com/laravel/framework/pull/53734 - [11.x] Include the initial value in the return types of
reduce()
by @lorenzolosa in https://github.com/laravel/framework/pull/53798 - [11.x] Add pingOnSuccessIf & pingOnFailureIf to Schedule handling by @lucacastelnuovo in https://github.com/laravel/framework/pull/53795
- [11.x] Improve PHPDoc for nullable properties in
IlluminateDatabaseQueryBuilder
class by @xurshudyan in https://github.com/laravel/framework/pull/53793 - [11.x] Remove usage of
compact()
in Container by @KennedyTedesco in https://github.com/laravel/framework/pull/53789 - [11.x] Make
Exceptions@dontTruncateRequestExceptions()
fluent by @cosmastech in https://github.com/laravel/framework/pull/53786 - [11.x] Make mailables tappable by @kevinb1989 in https://github.com/laravel/framework/pull/53788
- URI by @taylorotwell in https://github.com/laravel/framework/pull/53731
- [11.x] Require
laravel/serializable-closure
on Database component by @patrickcarlohickman in https://github.com/laravel/framework/pull/53822 - [11.x] use new PHP 8
str_
functions by @browner12 in https://github.com/laravel/framework/pull/53817 - [11.x] handle
password_hash()
failures better by @browner12 in https://github.com/laravel/framework/pull/53821 - [11.x] remove unnecessary
return
statement by @browner12 in https://github.com/laravel/framework/pull/53816 - [11.x] simplify passing arguments to
when()
by @browner12 in https://github.com/laravel/framework/pull/53815 - [11.x] remove redundant
array_values
call by @browner12 in https://github.com/laravel/framework/pull/53814 - [11.x] prefer assignment over
array_push
for 1 element by @browner12 in https://github.com/laravel/framework/pull/53813 - [11.x] fix
chopStart
andchopEnd
tests by @browner12 in https://github.com/laravel/framework/pull/53812 - [11.x] remove temporary variables by @browner12 in https://github.com/laravel/framework/pull/53810
- [11.x] fix
$events
docblock type by @browner12 in https://github.com/laravel/framework/pull/53808 - [11.x] Fix docblock for URI by @cosmastech in https://github.com/laravel/framework/pull/53804
- Bump nanoid from 3.3.7 to 3.3.8 in /src/Illuminate/Foundation/resources/exceptions/renderer by @dependabot in https://github.com/laravel/framework/pull/53831
- [11.x] use promoted properties by @browner12 in https://github.com/laravel/framework/pull/53807
- Revert “[11.x] use promoted properties” by @taylorotwell in https://github.com/laravel/framework/pull/53832
- Using throw config of filesystem disks when faking by @emulgeator in https://github.com/laravel/framework/pull/53779
- [11.x] Fix schema names on
DatabaseTruncation
trait (PostgreSQL and SQLServer) by @hafezdivandari in https://github.com/laravel/framework/pull/53787
The post URI Parsing and Mutation in Laravel 11.35 appeared first on Laravel News.
Join the Laravel Newsletter to get all the latest
Laravel articles like this directly in your inbox.
Source: Read MoreÂ