jeudi 9 juin 2016

Trying to abstract the Database class and stop it's public usage : Laravel 5.2.31

I have following classes

Interface

interface IRole {
    public function All();
}

In below class, I will also write some logic before sending the data to Database layer and some code after data is retrieved from Database class

class RoleBL implements IRole {

    public function All() {
        return (new RoleDb())->All();
    }
}

Database class

class RoleDb {
    public function All() {
        $Roles = \App\Models\Role\RoleModel
                ::all();
        return $Roles;
    }
}

Below is my Controller Action Method

class MembershipController extends \App\Http\Controllers\BaseController
{
    private $role;

    public function __construct(IRole $_role) {
        $this->role = $_role;
        parent::__construct();
    }

    public function create() {
        $RoleTypes = $this->role->All();
        return view('Membership.Create', array('Roles' => $RoleTypes));
    }
}

Can somebody help me how can I stop direct access to RoleDb class? It's access modifier is public right now.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire