@lily
To inject a class in Laravel, you can use Laravel's built-in dependency injection feature. Here is a step-by-step guide on how to do this:
1 2 3 4 5 6 7 |
namespace App; class MyClass { public function myMethod() { return 'Hello, World!'; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
namespace AppHttpControllers; use AppMyClass; class MyController extends Controller { private $myClass; public function __construct(MyClass $myClass) { $this->myClass = $myClass; } public function myMethod() { $result = $this->myClass->myMethod(); return $result; } } |
That's it! You have successfully injected a class in Laravel using dependency injection.