In laravel, the make method is used to parse a type from the container. The type is defined in the source code. The result returned after parsing is an instance of the type. When the container class calls the make method, if there is no The registered key will automatically instantiate the concrete class through the reflection class.
#The operating environment of this article: Windows 10 system, Laravel version 6, Dell G3 computer.
What is the role of the make method in laravel
The make method in Laravel is used to parse a type from the container. This type is defined in the source code and is not easy to translate into Chinese. The result returned after parsing is an instance of type.
Students who have read the source code should know that there is a make method in the Illuminate\Foundation\Application class and its parent class Illuminate\Container\Container class. Then when executing this line of code in index.php ,
1 $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
, it will first execute Illuminate\Foundation\Application make method, so let's take a look at it first. (This article takes the make Kernel class as an example)
/** * Resolve the given type from the container. 从容器当中解析给定的type * * (Overriding Container::make) 覆盖了父类中的make方法 * * @param string $abstract 给定的type * @param array $parameters 指定一些参数 可选项 * @return mixed */ public function make($abstract, array $parameters = []) { $abstract = $this->getAlias($abstract);//调用父类中的getAlias方法 //如果在deferredServices这个数组设置了这个type并且在instances数组中没有设置这个type if (isset($this->deferredServices[$abstract]) && ! isset($this->instances[$abstract])) { $this->loadDeferredProvider($abstract);//那么就执行这个方法:加载被定义为延迟的服务提供者 } return parent::make($abstract, $parameters);//调用父类的make方法 }
Okay, let’s go step by step. First, let’s take a look at the getAlias method. The function of this method is to return the alias of this class. If given If it is a complete class name and has been set in aliases, then the alias of the class name will be returned. If it has not been set, the class name itself will be returned. When looking at this method, you can var_dump $app first and compare it. The aliases array inside, the framework author wrote this method is really clever, at least this recursive method is rarely used in my actual development.
/** * Get the alias for an abstract if available. * * @param string $abstract * @return string * * @throws \LogicException */ public function getAlias($abstract) { if (! isset($this->aliases[$abstract])) { return $abstract; } if ($this->aliases[$abstract] === $abstract) { throw new LogicException("[{$abstract}] is aliased to itself."); } return $this->getAlias($this->aliases[$abstract]); }
The next step is to judge the two arrays of deferredServices and instances. In this example, $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); The result is false, so the loadDeferredProvider method is not executed.
The next step is to call the make method in the parent class Illuminate\Container\Container,
/** * Resolve the given type from the container. * * @param string $abstract * @param array $parameters * @return mixed */ public function make($abstract, array $parameters = []) { return $this->resolve($abstract, $parameters);//直接调用resolve方法 }
[Related recommendations: laravel video tutorial]
The above is the detailed content of What is the function of make method in laravel. For more information, please follow other related articles on the PHP Chinese website!

MigrationsinLaravelmanagedatabaseschema,whilemodelshandledatainteraction.1)Migrationsactasblueprintsfordatabasestructure,allowingcreation,modification,anddeletionoftables.2)Modelsrepresentdataandprovideaninterfaceforinteraction,enablingCRUDoperations

SoftdeletesinLaravelarebetterformaintaininghistoricaldataandrecoverability,whilephysicaldeletesarepreferablefordataminimizationandprivacy.1)SoftdeletesusetheSoftDeletestrait,allowingrecordrestorationandaudittrails,butmayincreasedatabasesize.2)Physica

SoftdeletesinLaravelareafeaturethatallowsyoutomarkrecordsasdeletedwithoutremovingthemfromthedatabase.Toimplementsoftdeletes:1)AddtheSoftDeletestraittoyourmodelandincludethedeleted_atcolumn.2)Usethedeletemethodtosetthedeleted_attimestamp.3)Retrieveall

LaravelMigrationsareeffectiveduetotheirversioncontrolandreversibility,streamliningdatabasemanagementinwebdevelopment.1)TheyencapsulateschemachangesinPHPclasses,allowingeasyrollbacks.2)Migrationstrackexecutioninalogtable,preventingduplicateruns.3)They

Laravelmigrationsarebestwhenfollowingthesepractices:1)Useclear,descriptivenamingformigrations,like'AddEmailToUsersTable'.2)Ensuremigrationsarereversiblewitha'down'method.3)Considerthebroaderimpactondataintegrityandfunctionality.4)Optimizeperformanceb

Single-page applications (SPAs) can be built using Laravel and Vue.js. 1) Define API routing and controller in Laravel to process data logic. 2) Create a componentized front-end in Vue.js to realize user interface and data interaction. 3) Configure CORS and use axios for data interaction. 4) Use VueRouter to implement routing management and improve user experience.

The steps to create a custom helper function in Laravel are: 1. Add an automatic loading configuration in composer.json; 2. Run composerdump-autoload to update the automatic loader; 3. Create and define functions in the app/Helpers directory. These functions can simplify code, improve readability and maintainability, but pay attention to naming conflicts and testability.

When handling database transactions in Laravel, you should use the DB::transaction method and pay attention to the following points: 1. Use lockForUpdate() to lock records; 2. Use the try-catch block to handle exceptions and manually roll back or commit transactions when needed; 3. Consider the performance of the transaction and shorten execution time; 4. Avoid deadlocks, you can use the attempts parameter to retry the transaction. This summary fully summarizes how to handle transactions gracefully in Laravel and refines the core points and best practices in the article.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!
