Framework: [L6] Route URL Generation fails when giving 0 as a Route Parmater Value

Created on 5 Dec 2019  路  6Comments  路  Source: laravel/framework

  • Laravel Version: 6.6.1
  • PHP Version: 7.3.1
  • Database Driver & Version: MySQL, MariaDB 10.4

Description:

It appears when passing through an integer 0 (or any falsey value) to the route() helper or any other route URL generation function, a UrlGenerationException is thrown.

It appears this issue is caused by the following change in this PR: https://github.com/laravel/framework/pull/30714/files#diff-9ffa9921c8542da8cb0059fcbc3ad48aR218-R231

Since this is doing a non-strict type check on the Route Parameter value, it always returns false when any falsey value is given to it (as a blank string is equal to false in non-strict checks)

Steps To Reproduce:

  1. Create a route with a required parameter and no default values, example:
    Route::get('/user/{id}/post/{post_id}', 'PostController@get')->name('user.post');
  2. Attempt to pass through 0 as one of the values, example:
    route('user.post', [ 'id' => 1, 'post_id' => 0 ]);
  3. This should product an exception similar to the following:
    Missing required parameters for [Route: user.post] [URI: user/{id}/post/{post_id}]. (View: ...)
bug

Most helpful comment

All 6 comments

Why would your post id be 0?

While it is unconventional, ids are typically unsigned integers. 0 is a valid id.

In one particular case, I have users that may or may not have accounts. An external service always attempts to load the user's account balance and doesn't handle null very well, so I use an account with id 0 that always has a balance of 0.

Yes that's true. I think this might be an easy fix to just check if it's 0 and let it pass through?

Funny I didn't use !empty just to avoid that and then I missed a =.
Sorry about that, I'll fix it later.

We did a new patch release with this fix.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lzp819739483 picture lzp819739483  路  3Comments

PhiloNL picture PhiloNL  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments

digirew picture digirew  路  3Comments

kerbylav picture kerbylav  路  3Comments