[SOLVED] Laravel 9 Undefined Constant illuminate\http\request::header_x_forwarded_all When Upgrading

by Dillon -

Laravel Trusted Proxies

Have you upgraded your Laravel application from Laravel 8 to Laravel 9 and received the ā€œUndefined Constant llluminate\Http\Request::HEADER_X_FORWARDED_ALLā€ error when running composer update?

The error is caused by Laravel dropping Fideloper/proxy in Laravel 9, and it can be easy to miss on the official documentation. The Laravel team states the likelihood of impact for this error is low, and thankfully it’s a quick and easy fix. 

In this post, you will understand what has changed under the hood of Laravel and its dependencies. You will learn how to fix the error “Undefined Constant llluminate\Http\Request::HEADER_X_FORWARDED_ALL” when running composer update.

What’s changed in Laravel 9?

The cause of the Undefined Constant llluminate\Http\Request::HEADER_X_FORWARDED_ALL error in Laravel 9

The cause of the HEADER_X_FORWARDED_ALL error comes from the Symfony HttpFoundation component used by Laravel. Laravel previously used HEADER_X_FORWARDED_ALL constant for the value of the $headers property in the TrustProxies.php file, however since Symfony 5.2 this was deprecated, and finally removed in version 6.0.

When upgrading to Laravel 9, composer upgrades dependencies as well. When the Symfony HttpFoundation package is upgraded, the Undefined Constant llluminate\Http\Request::HEADER_X_FORWARDED_ALL error is thrown.

The solution to fix the Laravel 9 Undefined Constant error

Follow the steps below to fix the Undefined constant Illuminate\Http\Request::HEADER_X_FORWARDED_ALLerror.

Within your app open /Http/Middleware/TrustProxies.php  and update line 5.

Use Fideloper\Proxy\TrustProxies as Middle;

Change this line to: 

Use Illuminate\Http\Middleware\TrustProxies as Middleware;

Next, you need to update the $headers property. 

Laravel Middleware Request Headers

protected $headers =

    Request::HEADER_X_FORWARDED_FOR |

    Request::HEADER_X_FORWARDED_HOST |

    Request::HEADER_X_FORWARDED_PORT |

    Request::HEADER_X_FORWARDED_PROTO |

    Request::HEADER_X_FORWARDED_AWS_ELB;

And finally, you can remove fideloper/proxy composer dependency from your application: 

composer remove fideloper/proxy

You can read more about the changes required when upgrading to Laravel 9 in the official upgrade guide under the Trusted Proxies subheading.

Conclusion

I hope this has helped you fix the Undefined Constant llluminate\Http\Request::HEADER_X_FORWARDED_ALLerror when upgrading to Laravel 9

If you are having any trouble please leave a comment below and I will try to help you solve the Undefined Constant llluminate\Http\Request::HEADER_X_FORWARDED_ALLerror.

Dillon Smart

šŸ‘‹ Hey, I'm Dillon!

Iā€™m a Full-Stack developer in the UK, building websites and applications for around 12 years, landing my first full-time job as a developer in 2015.

Feel free to reach out on Twitter!

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.