Twinkle
Aliases are often used in Laravel for brevity.
Route::middleware(['auth:sanctum', 'verified'])
auth
and verified
are aliases. The :sanctum
part is a middleware parameter.
In Laravel 11, it is defined here:
https://github.com/laravel/framework/blob/273b41ad4847ef036c9dc2a0537831bca2de687e/src/Illuminate/Foundation/Configuration/Middleware.php#L734
/**
* Get the default middleware aliases.
*
* @return array
*/
protected function defaultAliases()
{
$aliases = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
It is clearly described as middleware aliases, so these are aliases.
If you don't use aliases, it would be longer like this, which is why aliases are used to keep it short.
use Illuminate\Auth\Middleware\Authenticate::class;
use Illuminate\Auth\Middleware\EnsureEmailIsVerified::class;
Route::middleware([Authenticate::class.':sanctum', EnsureEmailIsVerified::class])
For standard Laravel middleware, you can write them shorter using aliases, but for project-specific middleware, you can use them directly without registering an alias.
Facade Aliases
Facades themselves serve as aliases, but these facades are also registered with aliases to make them shorter.
use Illuminate\Support\Facades\App;
use App;
However, nowadays it is not recommended to use use App;
. The basic approach is to specify and use use Illuminate\Support\Facades\App;
. This has been completely removed from the official documentation. The only exception is within the global space in Blade.
Short facades were used only during the transition from Laravel 4 to Laravel 5.
Common Misconceptions
There is a significant misconception that middleware and facades cannot be used without registering aliases.
The basic approach is to use Authenticate::class
directly without registering an alias.
Aliases are merely for writing shorter code and are not mandatory.
The premise of the discussion differs between the "framework side" and the "project side (user land)".
There is no benefit in writing shorter code for middleware that is used only once in a project.
https://laracasts.com/discuss/channels/livewire/no-understood-what-line-saymiddleware