Richard C.
—Suppose you have a route definition that was working in Laravel version 7 or older, such as:
Route::get('hello', array('uses' => 'HelloWorldController@sayHello'));
You will find that it generates errors in Laravel 8 and above. You will receive a runtime error like the following:
Illuminate \ Contracts \ Container \ BindingResolutionException Target class [SomethingController] does not exist.
Assume you have a Laravel controller like the one below:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HelloWorldController extends Controller { public function sayHello() { return "Hello World"; } }
In your web.php
routes file you can call this controller in any of the first four ways below, but the last two will not work:
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\HelloWorldController; # 1 Route::get('hello', [App\Http\Controllers\HelloWorldController::class, 'sayHello']); # 2 Route::get('hello', 'App\Http\Controllers\HelloWorldController@sayHello'); # 3 Route::get('hello', [HelloWorldController::class,'sayHello']); # 4 Route::get('hello', array('uses' => 'App\Http\Controllers\HelloWorldController@sayHello')); # Error! Route::get('hello', array('uses' => 'HelloWorldController@sayHello')); Route::get('hello', array('uses' => 'HelloWorldController::class@sayHello'));
Routes 1 and 2 both use the full path of the controller. Route 1 uses the array syntax and 2 uses the string syntax to point to the controller.
Route 3 is the same as route 1, except that it gives the controller name without a path, relying on it already being imported at the start of the file in the line use App\Http\Controllers\HelloWorldController;
. Without this import statement, you would have to use the full path of the controller or Laravel will throw an error.
Route 4 is a combination of routes 1 and 3: an array with a string path.
The final two routes will not work because they do not use the full controller path. This was acceptable in Laravel before version 8 but is no longer allowed. For more about this change, read the Laravel 8 upgrade guide.
Always use the style of route 1. Here you give the full name of the controller and its type. This is easy to refactor and automated tools can check for errors. The magic string style used in routes 2 and 4 should not be used and is difficult to refactor. These syntaxes are from older versions of Laravel.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.