Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly does Laravel make class alias possible?

Tags:

php

laravel

What I mean is, how is it you can access the Validator facade without using the FQN in a controller?

Ie, why does validator::make work, not requiring illuminate\blah\facades\validator::make()

How is this accomplished from a PHP standpoint? FYI, I'm not interested in how to alias a class through Laravel's App class, but rather the mechanics of it. Is it done through a custom autoloading function...?

like image 209
Rick Sinders Avatar asked Oct 24 '25 03:10

Rick Sinders


1 Answers

To be precise it's implemented in:

\vendor\laravel\framework\src\Illuminate\Foundation\AliasLoader.php

file, using class_alias() function as @user3158900 mentioned:

public function load($alias)
{
    if (isset($this->aliases[$alias]))
    {
        return class_alias($this->aliases[$alias], $alias);
    }
}
like image 174
Marcin Nabiałek Avatar answered Oct 26 '25 16:10

Marcin Nabiałek