0

更新:如果我可以使用不同的方法實現相同的結果,請賜教。使用IoC進行laravel依賴注入的路由嵌套控制器3

我在構建我的項目時使用/學習laravel 3。在編寫任何頁面內容之前,我正在驗證是否可以按計劃部署所有內容,因爲這個項目實際上是重寫了一個相當龐大的應用程序,它在使用的技術上嚴重過時。

我在這最後一部分掙扎着,這可能是我設置我的項目時面臨的最困難的挑戰。

網址:

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

以上是我想要的代碼大氣壓的URI。 的_ 標識符 _部分應該成爲一個模型(基於口才)

商店爲嵌套控制器

即:

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

每個現有URI店/ 標識符是一個真正的網站。 (雖然它有不同的領域offcourse) 我想我所有的嵌套商店控制器知道他們正在與什麼店。事實上,標識符將用於加載正確的佈局,以呈現正確的圖像,聯繫方式等... 從我讀過的,我將需要使用IoC功能注入的依賴關係我的商店模型到我的控制器的構造函數。

這是我有個大氣壓:

文件:應用/ start.php

/** 
* 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); 
}); 

文件:應用/ routes.php文件

/** 
* 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; 
}); 

店基地 - 位於應用程序/庫/控制器/ s的控制器 hop.php

<?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(); 
    } 

} 

文件:應用/控制器/店/ home.php

<?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'); 
    } 

} 

問題:我定義這些嵌套商店控制器路由時

  • 。我是否簡單地返回控制器laravel應該用來解決請求,還是我在該路由定義的回調函數中自己觸發操作?
  • 控制器不是自動加載(當嘗試上面的實現時),但我使用這些控制器的正確約定(除非我失去了一些東西:-))。我猜這是因爲我使用IoC,我如何幹淨地實現這個或我的錯誤是什麼?
  • 我如何觸發正確的操作?它應該像預期的那樣觸發相應的HTTP動作動作,因爲我的嵌套控制器也是RESTFUL控制器。
  • 額外的問題,以儘可能保持乾淨:'索引'默認爲不使用IoC功能時的家庭控制器。我的解決方案(route.php中的if條件)是否將此功能模擬爲乾淨的?還是有更好的方法?

通過一切手段:

  • 如果我的做法是路要走,請告訴我,我在laravel一個新手,這是我使用的第一個框架,讓我在一般的框架中,這是一個新手。

  • 我也想道歉,如果我的問題沒有解釋得很好,所以隨時提出額外的信息。

  • 我試着用google搜索這個問題,但是找不到類似的東西,這是第一個,因爲我所有的其他laravel問題都可以通過google輕鬆解決。

我非常感謝任何花時間閱讀本文的人,甚至更好地向正確的方向發送我!

回答

0

好的,最終的解決方案,如果你問我,這是遠遠不乾淨的,但它似乎工作,至少據我檢查。

我也不能肯定是否有替代方案,但由於缺乏響應和時間壓力,我決定去與此並保持手指交叉:(。

//start.php

IoC::register('controller: shop', function($id, $controllerName){ 
    //controller name is the name of the controller located in the 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 to return this, and i now need to manually return every response in every action in every shop controller 
     */ 
    return call_user_func_array(array($controller, $http_verb . '_' . $action), $params); 
}); 

所以示例給出

class Shops_Home_Controller extends Shop_Controller 
{ 

    public function get_index(){ 
     /** 
     * this works when doing things the usual way, but will not return any output 
     * when working with nested dependency injection 
     */ 
     $this->layout->nest('content', 'shops.index'); 
    } 

    public function get_test(){ 
     /** 
     * this needs to return layout object if it is to work with the nested dependency injection 
     */ 
     $this->layout->nest('content', 'shops.index'); 
    } 

}