Skip to content

Commit

Permalink
[8.x] Fix route groups with no prefix on PHP 8.1 (laravel#39115)
Browse files Browse the repository at this point in the history
* PHP 8.1, fix route groups with no prefix.

In php 8.1 calling trim() on null values is deprecated and since my route collection have groups without a defined prefix, I get an error on this line of code.
Parsing in an empty string as opposed to null is the minimal change which solves the problem.

* Added test cast for grouped route without prefix
  • Loading branch information
GuardsmanPanda authored Oct 7, 2021
1 parent 1557042 commit a683198
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/RouteGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected static function formatNamespace($new, $old)
*/
protected static function formatPrefix($new, $old, $prependExistingPrefix = true)
{
$old = $old['prefix'] ?? null;
$old = $old['prefix'] ?? '';

if ($prependExistingPrefix) {
return isset($new['prefix']) ? trim($old, '/').'/'.trim($new['prefix'], '/') : $old;
Expand Down
10 changes: 10 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ public function testCanRegisterGroupWithDomainAndNamePrefix()
$this->assertSame('api.users', $this->getRoute()->getName());
}

public function testRouteGroupingWithoutPrefix()
{
$this->router->group([], function ($router) {
$router->prefix('bar')->get('baz', ['as' => 'baz', function () {
return 'hello';
}]);
});
$this->seeResponse('hello', Request::create('bar/baz', 'GET'));
}

public function testRegisteringNonApprovedAttributesThrows()
{
$this->expectException(BadMethodCallException::class);
Expand Down

0 comments on commit a683198

Please sign in to comment.