Note that there are some explanatory texts on larger screens.

plurals
  1. PORouting nested controllers using IoC for dependency injection in laravel 3
    text
    copied!<blockquote> <p><strong>update:</strong> if i can achieve same result using a different approach, please enlighten me.</p> </blockquote> <p>I'm using/learning laravel 3 while building my project. Before coding any page-content at all, I'm verifying if i can deploy everything as planned, since this project is an actual rewrite of a rather huge app which is seriously outdated in the techniques it uses.</p> <p>I'm struggling at this last part, which is quite possibly the hardest challenge i'll face to setup my project.</p> <p>URL:</p> <pre><code>site.com/shops/__identifier__/controller/action/params </code></pre> <p>The above is the uri i'm trying to code atm. The <strong>_<em>identifier</em>_</strong> part should become a model (eloquent based)</p> <p>the <strong>shops</strong> is the base for nested controllers</p> <p>ie:</p> <pre><code>controllers/ - shops/ - home.php - contact.php - products.php - etc .... </code></pre> <p>Each existing uri shops/<strong>identifier</strong> is a real site on its own. (though it has a different domain offcourse) I want all my nested shops controllers to know what shop they're working with. In fact, the identifier will be used to load the correct layouts, to render the correct images, contact details etc... From what i've read, i'll need to use the <strong>IoC</strong> functionality to inject the dependency of my <strong>shop-model</strong> into the constructor of my controller.</p> <p>this is what i have atm:</p> <p>file:<strong>application/start.php</strong></p> <pre><code>/** * Register IoC container for my nested shop controllers */ IoC::register('controller: shop', function($controller, $identifier) { //also tried using same line without the \\ $class = '\\Shops_' . ucfirst($controller) . '_Controller'; return new $class($identifier); }); </code></pre> <p>file:<strong>application/routes.php</strong></p> <pre><code>/** * Register all 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; }); </code></pre> <p><strong>shop base-controller</strong> located at application/libraries/controllers/shop.php</p> <pre><code>&lt;?php namespace Controllers; use Base_Controller; /** * Shop controller */ class Shop extends Base_Controller { public function __construct($identifier){ /** * @todo: load the shop model using the identifier * possibly move this after the parent::__construct() */ parent::__construct(); } } </code></pre> <p>file: <strong>applications/controllers/shops/home.php</strong></p> <pre><code>&lt;?php /** * @heads up: Shop_Controller is aliased in application/config/application.php */ class Shops_Home_Controller extends Shop_Controller { public function get_index(){ return ('test'); } } </code></pre> <p><strong>Problems:</strong></p> <ul> <li>when defining my routes for these nested shops controllers. Do i simply return the controller laravel should use to resolve the request, or do i trigger the action myself in the callback function in that route definition?</li> <li>controllers aren't autoloading (when trying the implementation above), yet i'm using the correct conventions for those controllers (unless i'm missing something :-) ). I'm guessing this is because i'm using IoC, how do i cleanly implement this or what is my mistake?</li> <li>how do i trigger the correct action? It should, as expected, trigger the corresponding HTTP-verb action, since my nested controllers are also RESTFUL-controllers.</li> <li>extra question to keep things as clean as possible: 'index' defaults to home controller when not using IoC functionality. Is my solution (if-condition in routes.php) to mimic this functionality a clean one? or is there any better approach?</li> </ul> <p><strong>By all means:</strong></p> <ul> <li><p>If my approach is way off, please tell me, i'm a newbie at laravel, and it's the first framework i'm using, so i'm a newbie at frameworks in general.</p></li> <li><p>I'd also like to apologize if my question isn't explained to well so feel free to ask extra info.</p></li> <li><p>I tried my best at googling this problem, but couldn't find anything similar, which is a first, since all my other laravel problems were easily solved using google.</p></li> </ul> <p>I'd kindly thank anyone taking the time to read this and even better send me in the right direction!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload