1.

Explain Laravel service container.

Answer»

Laravel service container is a great and POWERFUL tool to manage dependencies and to perform dependency INJECTION. So now the question comes what is dependency injection - In classes dependencies will be injected through either constructor or setter method.

<?php namespace App\Http\Controllers; use App\Employee; use App\Repositories\EmployeeRepository; use App\Http\Controllers\Controller; class EmployeeController extends Controller {    /**     * The employee repository implementation.     *     * @var EmployeeRepository     */    protected $employees;    /**     * Create a new controller instance.     *     * @param  EmployeeRepository  $employees     * @RETURN void     */    public function __construct(EmployeeRepository $employees)    {        $this->employees = $employees;    }    /**     * Show the profile for the given employee.     *     * @param  int $id     * @return Response     */    public function show($id)    {        $employee = $this->employees->find($id);        return view('employee.profile', ['employee' => $employee]);    } }

Here EmployeeController NEEDS to retrieve INFORMATION of employee from a data source so we have injected a service that will provide information of the employee.



Discussion

No Comment Found