php - Unresolvable dependency resolving in Laravel Repository -
i'm having "unresolvable dependency resolving [parameter #1 [ $mainpage ]]" error after trying manage dependency injection in repository. i've managed build controller, repository, interface , service provider. missing here? i'm doing wrong in logic?
home controller
use repositories\mainpagerepository; class homecontroller extends basecontroller { protected $cpage, $mainpage; public function __construct(mainpagerepository $cpage, $mainpage) { $this->cpage = $cpage; $this->mainpage = $mainpage; } public function getpages($lang, $slug) { $cpage = $this->cpage->getcurrentpage($lang, $slug); $mainpages = $this->mainpage->getallmainpages($cpage); $allpages = $this->getallpages($mainpages); return view::make('index') ->with('cpage', $cpage) ->with('mainpages', $mainpages) ->with('allpages', $allpages); } private function getallpages($pages) { $allpages = array(); foreach ($pages $page) { //... } return $allpages; }
mainpageinterface
namespace repositories; interface mainpageinterface { public function getcurrentpage($lang, $slug); public function getallmainpages($cpage); }
mainpagerepository
namespace repositories; use page; class mainpagerepository implements mainpageinterface{ public function getcurrentpage($lang, $slug) { $cpage = page::join('langs', 'langs.id', '=', 'pages.lang_parent_id') ->where('slug', '=', $slug) ->where('code', '=', $lang) ->first(); return $cpage; } public function getallmainpages($cpage) { $mainpage = page::join('langs', 'langs.id', '=', 'pages.lang_parent_id') ->where('parent_id', null) ->get(); return $mainpage; } }
mainpageserviceprovider
namespace repositories; use illuminate\support\serviceprovider; class mainpageserviceprovider extends serviceprovider { public function register() { $this->app->bind('mainpageinterface', 'mainpagerepository'); } }
implemented changes after @matt burrows answer: problem still persists controller
use repositories\mainpageinterface; class homecontroller extends basecontroller { protected $cpage, $mainpage; public function __construct(mainpageinterface $cpage, $mainpage) { $this->cpage = $cpage; $this->mainpage = $mainpage; }
service provider
public function register() { $this->app->bind('repositories\mainpageinterface', 'repositories\mainpagerepository'); }
ok, managed on splitting 2 functions in repository 2 seperate repositories. did trick. i'll accept @matts answer correct in case, since spotted important mistakes nonetheless.
change
$this->app->bind('mainpageinterface', 'mainpagerepository');
to
$this->app->bind('repositories\mainpageinterface', 'repositories\mainpagerepository');
within service provider.
also change
use repositories\mainpagerepository;
to
use repositories\mainpageinterface;
and change
public function __construct(mainpagerepository $cpage, $mainpage)
to
public function __construct(mainpageinterface $cpage, $mainpage)
and should work.
remember register service provider within app.php if have not done already.
this can done adding class path array of providers so;
'providers' => array( //... 'repositories\mainpageserviceprovider' )
be sure composer dump-autoload
too
for problem of unresolvable dependency resolving [parameter #1 [ $mainpage ]]
because ioc container not know type of object $mainpage is. type cast object , want.
this can done so;
use path\to\a\object; ... __construct(mainpagerepository $cpage, object $mainpage)
Comments
Post a Comment