Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dependency injection 在laravel 3中使用IoC为依赖项注入路由嵌套控制器_Dependency Injection_Routing_Controller_Inversion Of Control_Laravel 3 - Fatal编程技术网

Dependency injection 在laravel 3中使用IoC为依赖项注入路由嵌套控制器

Dependency injection 在laravel 3中使用IoC为依赖项注入路由嵌套控制器,dependency-injection,routing,controller,inversion-of-control,laravel-3,Dependency Injection,Routing,Controller,Inversion Of Control,Laravel 3,更新:如果我可以使用不同的方法获得相同的结果,请告诉我 在构建项目时,我正在使用/学习laravel 3。在编写任何页面内容之前,我要验证我是否能够按计划部署所有内容,因为这个项目实际上是对一个相当庞大的应用程序的重写,它使用的技术已经严重过时 我正在努力完成最后一部分,这可能是我在设置项目时面临的最困难的挑战 网址: 上面是我试图编码atm的uri。 \u标识符部分应成为模型(基于雄辩) 商店是嵌套控制器的基础 即: 每个现有uri存储/标识符本身就是一个真实的站点。(当然,它有一个不同的领域

更新:如果我可以使用不同的方法获得相同的结果,请告诉我

在构建项目时,我正在使用/学习laravel 3。在编写任何页面内容之前,我要验证我是否能够按计划部署所有内容,因为这个项目实际上是对一个相当庞大的应用程序的重写,它使用的技术已经严重过时

我正在努力完成最后一部分,这可能是我在设置项目时面临的最困难的挑战

网址:

上面是我试图编码atm的uri。 \u标识符部分应成为模型(基于雄辩)

商店是嵌套控制器的基础

即:

每个现有uri存储/标识符本身就是一个真实的站点。(当然,它有一个不同的领域) 我想让我所有的嵌套商店控制器都知道他们正在使用哪个商店。事实上,标识符将用于加载正确的布局、呈现正确的图像、联系人详细信息等。。。 根据我所读的内容,我需要使用IoC功能将我的商店模型的依赖项注入控制器的构造函数中

这就是我的atm机:

文件:application/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);
});
/**
 * 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;
});
<?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');
    }

}
文件:application/routes.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);
});
/**
 * 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;
});
<?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');
    }

}
商店基础控制器位于application/libraries/controllers/shop.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();
    }

}

好的,最终解决方案,如果你问我的话,这远不是干净的,但它似乎有效,至少我已经检查过了

我也不确定是否有其他选择,但由于缺乏回应和时间压力,我决定继续这样做,并祈祷:(

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

}