In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the example code of Laravel class and interface injection". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the example code of Laravel class and interface injection".
Laravel can automatically inject needed dependencies, which is somewhat different for custom classes and interfaces.
For classes, Laravel can be injected automatically, but for interfaces, you need to create the corresponding ServiceProvider registration interface and implement the binding of the class, and you need to add ServiceProvider to congif/app.php 's providers array, so that the container can know which implementation you need to inject.
Now customize a class myClass
Namespace App\ library
Class myClass {public function show () {echo _ _ FUNCTION__.' Hello World';}}
Set up route
Route::get ('test/ioc',' TestController@index')
Modify TestController
Class TestController extends Controller {public function index (myClass $myClass) {$myClass- > show ();}}
Visit http://localhost/test/ioc to print show Hello World successfully.
Modify myClass
Class myClass implements like {public function play () {/ / TODO: Implement play () method. Echo _ _ FUNCTION__.' Hello Play';}}
Like interface
Interface like {public function play ();}
TestController
Class TestController extends Controller {public function index (like $like) {$like- > play ();}}
If you still access the above address, you will get an error.
Target [App\ library\ like] is not instantiable.
For interface injection, we need to register in the register method of the corresponding ServiceProvider and write the corresponding ServiceProvider to the providers array of config/app.
Define LikeServiceProvider
Class LikeServiceProvider extends ServiceProvider {public function boot () {/ /} public function register () {/ / $this- > app- > bind ('App\ library\ like',' App\ library\ myClass');}}
Then we need to add LikeServiceProvider to the providers array in the config\ app.php file.
Or continue to visit the above address, the page successfully output play Hello Play.
Thank you for reading, the above is the content of "instance code of Laravel class and interface injection". After the study of this article, I believe you have a deeper understanding of the problem of Laravel class and interface injection instance code, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.