Routing nested controllers using IoC for dependency injection in laravel 3 -


update: if can achieve same result using different approach, please enlighten me.

i'm using/learning laravel 3 while building project. before coding page-content @ all, i'm verifying if can deploy planned, since project actual rewrite of rather huge app outdated in techniques uses.

i'm struggling @ last part, quite possibly hardest challenge i'll face setup project.

url:

site.com/shops/__identifier__/controller/action/params 

the above uri i'm trying code atm. _identifier_ part should become model (eloquent based)

the shops base nested controllers

ie:

controllers/   - shops/       - home.php       - contact.php       - products.php       - etc .... 

each existing uri shops/identifier real site on own. (though has different domain offcourse) want nested shops controllers know shop they're working with. in fact, identifier used load correct layouts, render correct images, contact details etc... i've read, i'll need use ioc functionality inject dependency of shop-model constructor of controller.

this have atm:

file:application/start.php

/** * register ioc container nested shop controllers */ ioc::register('controller: shop', function($controller, $identifier) {     //also tried using same line without \\     $class = '\\shops_' . ucfirst($controller) . '_controller';     return new $class($identifier); }); 

file:application/routes.php

/**  * register shop routes  */ route::any('/shops/(:any)/(:any?)/(:any?)', function($identifier, $controller = "home", $method = "index", $params = array()){     if($controller === "index")         $controller = "home";     $controller = ioc::resolve('controller: shop', array($controller, $identifier));     return $controller; }); 

shop base-controller located @ application/libraries/controllers/shop.php

<?php namespace controllers; use base_controller; /** * shop controller */ class shop extends base_controller {      public function __construct($identifier){         /**          * @todo: load shop model using identifier          * possibly move after parent::__construct()          */         parent::__construct();     }  } 

file: applications/controllers/shops/home.php

<?php /**  * @heads up: shop_controller aliased in application/config/application.php  */ class shops_home_controller extends shop_controller  {      public function get_index(){         return ('test');     }  } 

problems:

  • when defining routes these nested shops controllers. return controller laravel should use resolve request, or trigger action myself in callback function in route definition?
  • controllers aren't autoloading (when trying implementation above), yet i'm using correct conventions controllers (unless i'm missing :-) ). i'm guessing because i'm using ioc, how cleanly implement or mistake?
  • how trigger correct action? should, expected, trigger corresponding http-verb action, since nested controllers restful-controllers.
  • extra question keep things clean possible: 'index' defaults home controller when not using ioc functionality. solution (if-condition in routes.php) mimic functionality clean one? or there better approach?

by means:

  • if approach way off, please tell me, i'm newbie @ laravel, , it's first framework i'm using, i'm newbie @ frameworks in general.

  • i'd apologize if question isn't explained feel free ask info.

  • i tried best @ googling problem, couldn't find similar, first, since other laravel problems solved using google.

i'd kindly thank taking time read , better send me in right direction!

ok, final solution, far clean if ask me, seems work, @ least far i've checked.

i'm not sure if there alternative, due lack of response , timepressure, decided go , keep fingers crossed :(.

//start.php

ioc::register('controller: shop', function($id, $controllername){     //controller name name of controller located in shops map     $class = "shops_" . ucfirst($controllername) . "_controller";     include(path('app') . 'controllers/shops/' . strtolower($controllername) . '.php');     return new $class($id); }); 

//routes.php

route::any("shops/(:any)/(:any?)/(:any?)/(:all?)", function($id, $controller = "index", $action = "index", $params = ""){     if($controller === "index")         $controller = "home";     $params = explode('/', $params);     $controller = ioc::resolve("controller: shop", array($id, $controller));     $http_verb = request::method();         /**          * need return this, , need manually return every response in every action in every shop controller          */     return call_user_func_array(array($controller, $http_verb . '_' . $action), $params); }); 

so example given

class shops_home_controller extends shop_controller  {      public function get_index(){         /**          * works when doing things usual way, not return output           * when working nested dependency injection          */         $this->layout->nest('content', 'shops.index');     }      public function get_test(){         /**          * needs return layout object if work nested dependency injection          */         $this->layout->nest('content', 'shops.index');     }  } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -