Good day guys I am quite new to Laravel and I faced the problem with understanding of the need for aliases and providers which are located in config/app.php file. Also, why ::class is used at the end of each alias and provider like
'App' => Illuminate\Support\Facades\App::class, Illuminate\Auth\AuthServiceProvider::class
what if to omit ::class.
Please guys can you explain in simple terms
::class
it's a way to tell the code to read the actual name of the class and not the class itself. If you had, for example, added use Illuminate\Support\Facades\App
to the header of the file, you would be able to use simply by doing App::class
, which could, in the future, prevent a lot of replaces in the code, so you would need to replace only the header line. And also, if you don't use ::class
you will have to use a string fully qualified for the class name.
Well, you might actually not need the aliases, but Laravel makes use of them internally for several operations.
According to their documentation on Facades:
In the context of a Laravel application, a facade is a class that provides access to an object from the container
Which means that you can access any of the aliases methods statically.
And as of the documentation on Service Providers:
Service providers are the central place of all Laravel application bootstrapping. In general, registering things, including registering service container bindings, event listeners, middleware, and even routes
Which will then be necessary if you desire specific data to be loaded before anything else happens on the system, registering different services, downloaded components sometimes.
Some of this are invisible for you if you don't have a good practice working with them, some might be necessary if you decide to transform some part of your code into stand-alone modules.
But mainly, the necessity of them is mainly on Laravel side than on the developer, that is, considering how experienced you are with the Framework and(or) Bootstraping/Service Containing.
adding to @Rafaels answer. regarding the ::class
syntax.
This feature came in php 5.5, So that you dont have to keep your class names in strings. the Classname::class
will give the full class path
use App\Services\Service1\ServiceClass1;
//...
// suppose you want an array of some classes you can use this syntax
public $realation= [
Serviceclass1::class, // Equivalent to "App\Services\Service1\ServiceClass1"
];
from the doc
It is possible to use ClassName::class to get a fully qualified name of class ClassName. For example:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With