laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.55k stars 11.03k forks source link

UrlGenerationException sends a log line to stderr that breaks artisan serve #53534

Open jpbarbosa opened 1 day ago

jpbarbosa commented 1 day ago

Laravel Version

11.32.0

PHP Version

8.2.25

Database Driver & Version

No response

Description

When using a helper {{ route('anyroute') }} without the necessary route parameters (by accident), Laravel throws a UrlGenerationException, sending a log line to stderr that breaks the server.

The issue is the datetime string at the beginning, which getRequestPortFromLine is not prepared to parse. In Laravel 10, the same line does not display that datetime.

The problem only occurs when using LOG_CHANNEL=stderr.

[2024-11-16 06:58:05] local.ERROR: Undefined array key 1 {"exception":"[object] (ErrorException(code: 0): Undefined array key 1 at /Users/.../laravel-11/vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php:383)

...

ErrorException 

Undefined array key 1

at vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php:383
  379▕     protected function getRequestPortFromLine($line)
  380▕     {
  381▕         preg_match('/:(\d+)\s(?:(?:\w+$)|(?:\[.*))/', $line, $matches);
  382▕ 
➜ 383▕         return (int) $matches[1];
  384▕     }
  385▕ 
  386▕     /**
  387▕      * Get the console command options.

    +26 vendor frames 

27  artisan:13
    Illuminate\Foundation\Application::handleCommand(Object(Symfony\Component\Console\Input\ArgvInput))
Browser After reload Terminal

Steps To Reproduce

Install Laravel 11

composer create-project --prefer-dist laravel/laravel laravel-11
cd laravel-11

Change LOG_CHANNEL to stderr

# .env

LOG_CHANNEL=stderr

Create routes with any parameter

// routes/web.php

Route::view('/tenants/{tenant}', 'home')->name('home');
Route::View('/tenants/{tenant}/info', 'info')->name('info');

Create their views with routes missing required parameters

// resources/views/home.blade.php

<html>
    <body>
        <a href="{{ route('info') }}">Info</a>
    </body>
</html>
// resources/views/info.blade.php

<html>
    <body>
        <a href="{{ route('home') }}">Home</a>
    </body>
</html>

Run the server

php artisan serve

Access any route with parameter

open http://localhost:8000/tenants/tenant1

Server will throw an error, exit to the command line, and stop working

[2024-11-16 06:58:05] local.ERROR: Undefined array key 1 {"exception":"[object] (ErrorException(code: 0): Undefined array key 1 at /Users/.../laravel-11/vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php:383)

...

ErrorException 

Undefined array key 1

at vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php:383
  379▕     protected function getRequestPortFromLine($line)
  380▕     {
  381▕         preg_match('/:(\d+)\s(?:(?:\w+$)|(?:\[.*))/', $line, $matches);
  382▕ 
➜ 383▕         return (int) $matches[1];
  384▕     }
  385▕ 
  386▕     /**
  387▕      * Get the console command options.

    +26 vendor frames 

27  artisan:13
    Illuminate\Foundation\Application::handleCommand(Object(Symfony\Component\Console\Input\ArgvInput))
ahmad-cit22 commented 19 hours ago

I tried to address this issue & proposed a solution in this PR: #53538

Hope that it can be helpful in solving the issue.