Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inject model in laravel controllers constructor

I want to know if this is a good practice to use my model class in controllers in this way :

public function __construct(Rule $rules)
{
    $this->rules = $rules;
}

I do not want to repeat myself in my controllers so I want to know what is the best approach for that

like image 336
Mohsen Jalalian Avatar asked Mar 14 '18 08:03

Mohsen Jalalian


1 Answers

You use Dependency Injection - it is very good practice.

According to documentation: Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.

namespace App\Http\Controllers;

use App\User;
use App\Repositories\UserRepository;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * The user repository implementation.
     *
     * @var UserRepository
     */
    protected $users;

    /**
     * Create a new controller instance.
     *
     * @param  UserRepository  $users
     * @return void
     */
    public function __construct(UserRepository $users)
    {
        $this->users = $users;
    }

    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $user = $this->users->find($id);

        return view('user.profile', ['user' => $user]);
    }
}

In this example, the UserController needs to retrieve users from a data source. So, we will inject a service that is able to retrieve users. In this context, our UserRepository most likely uses Eloquent to retrieve user information from the database. However, since the repository is injected, we are able to easily swap it out with another implementation. We are also able to easily "mock", or create a dummy implementation of the UserRepository when testing our application.

Read also about Service Container - it is powerful tool: https://laravel.com/docs/5.6/container

like image 169
Adam Kozlowski Avatar answered Nov 20 '22 21:11

Adam Kozlowski