Laravel Undefined Constant header_x_forwarded_all after upgrading Laravel version

By Dillon Smart · · · 5 Comments

Laravel undefined constant error

Upgrading to a newer version of Laravel can be exciting, but it can also be nerve racking if you are unfamiliar with the changes in newer version, especially if your upgrading multiple times. Have you received an “Undefined Constant” error in Laravel after upgrading?

In this post, I will help you understand the changes under the hood in Laravel and it’s dependencies. We will learn how to fix the error “Undefined Constant llluminate\Http\Request::HEADER_X_FORWARDED_ALL” caused when running composer update.

Undefined Constant llluminate\Http\Request::HEADER_X_FORWARDED_ALL

The error is caused by Laravel dropping the Fideloper/proxy package from Laravel 9, and it can be easy to miss on the official documentation. As of Laravel 9, Fideloper/proxy has been incorporated into the core of Laravel.

You can find more information on this here.

The Laravel team states the likelihood of impact for this error is low, and thankfully it’s a quick and easy fix. 

What’s changed in Laravel?

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

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 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

Learn more about Composer

You can read more about the changes required when upgrading to Laravel 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

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.

Laravel

5 Comments

Tiago Farias

Great!!!

karthick

thank you it helped me

Paul

Thank you!!

Mohammed Rahat Hossain

Thanks this post helps me a lot

Erick Jeronimo

Thanks a lot!

Was this helpful? Leave a comment!

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

Laravel 419 page expired after login [SOLVED]

Updated 1st January 2024

Laravel is the most popular PHP framework, dominating the space since 2014, and the chosen framework by many new PHP developers. Many new developers run into the same errors when first using the framework, the most common being a 419 Page Expired error on a form post.  What is Cross-Site Request Forgery (CSRF)? Cross-site request

Laravel php artisan make:auth command not defined

Updated 11th August 2022

Laravel 6.0 LTS came with a lot of changes to the framework, apart from moving the the new Semantic Versioning, Laravels famous php artisan make:auth command was dropped. php artisan make:auth Before the Laravel make:auth command would create all the Migrations, Models, Controllers and Routes needed to easily setup an authenticatable application in seconds. However,

Laravel 9: How to Add a New Column to an Existing Table in a Migration

Updated 1st October 2023

Laravel has a number of features designed to make developing web applications easier and faster. Migrations in Laravel allows us to manage the structure of our databases. In this post, you will learn how to add a new column to an existing table in a migration. Create a new migration file First, we need to