Registering services in the Container

- We can easily replace a component with one created by ourselves or a third party.
- We have full control of the object initialization, allowing us to set these objects, as needed before delivering them to components.
- We can get global instances of components in a structured and unified way.

Services can be registered using serverial types of definitions:

<?php

use Phalcon\Http\Request;
    // Create the Dependency Injector Container
    $di = new Phalcon\Di();
    // By its class name
    $di->set("request", "Phalcon\Http\Request");
    
    // Using an anonymous function, the instance will be lazy loaded
    $di->set("request", function () {
        return new Request();
    });
    
    // Regsitering an instance directly
    $di->set("request", new Request());
    
    // Using an array definition
    $di->set(
        "request",
        array(
            "className" => 'Phalcon\Http\Request'
        )
    );
 
<?php
 
 // The array syntax is also allowed to register service:
    
    use Phalcon\Http\Request;
    // Create the Dependency Injector Container
    $di = new Phalcon\Di();
    
    // By its class name
    $di['request'] = 'Phalcon\Http\Request';
    // Using an anonymous function, the instance will be lazy loaded
    $di['request'] = function () {
        return new Request();
    };
    
    // Registering an instance directly
    $di["request"] = new Request();
    
    // Using an array definition
    $di['request'] = array(
        "className" => 'Phalcon\Http\Request'
    );
    
    
Models
    
    A model represents the information (data) of the application and the rules to manipulate that data. Models are primaryly used for managing the rules of interaction with a corresponding database table. In most cases, each table in your database will correspond to one model in your application. The bulk of your application's business logic will be concentrated in the models.
    
Views
    
    view represent the user interface of your applicaiton. Views are often HTML files with embeded PHP coe that perform tasks related solely to the presentation of the data. Views handle the job of providing data to the web browser or other tool that is used to make requests from your application.
    
 Controllers
 
    The controllers provide the "flow" between models and views. Controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.

[Phalcon-framework] Phalcon framework - Dependency Injection/Service Location And the MVC architecture note 1的更多相关文章

  1. Benefits of Using the Spring Framework Dependency Injection 依赖注入 控制反转

    小结: 1. Dependency Injection is merely one concrete example of Inversion of Control. 依赖注入是仅仅是控制反转的一个具 ...

  2. 【转】Understanding Inversion of Control, Dependency Injection and Service Locator Print

    原文:https://www.dotnettricks.com/learn/dependencyinjection/understanding-inversion-of-control-depende ...

  3. Zend Framework 2中如何使用Service Manager

    end Framework 2 使用ServiceManager(简称SM)来实现控制反转(IoC).有很多资料介绍了service managers的背景,我推荐大家看看this blog post ...

  4. Inversion of Control Containers and the Dependency Injection pattern(转)

    In the Java community there's been a rush of lightweight containers that help to assemble components ...

  5. Inversion of Control Containers and the Dependency Injection pattern

    https://martinfowler.com/articles/injection.html One of the entertaining things about the enterprise ...

  6. Inversion of Control Containers and the Dependency Injection pattern--Martin Fowler

    原文地址:https://martinfowler.com/articles/injection.html n the Java community there's been a rush of li ...

  7. Dependency Injection 筆記 (2)

    续上集,接着要说明如何运用 DI 来让刚才的范例程序具备执行时期切换实现类型的能力. (本文摘自電子書<.NET 依賴注入>) 入门范例—DI 版本 为了让 AuthenticationS ...

  8. asp.net core 系列之Dependency injection(依赖注入)

    这篇文章主要讲解asp.net core 依赖注入的一些内容. ASP.NET Core支持依赖注入.这是一种在类和其依赖之间实现控制反转的一种技术(IOC). 一.依赖注入概述 1.原始的代码 依赖 ...

  9. ASP.NET Core 的 Dependency Injection

    ASP.NET Core使用了大量的DI(Dependency Injection)设计,有用过Autofac或类似的DI Framework对此应该不陌生.本篇将介绍ASP.NET Core的依赖注 ...

随机推荐

  1. dedecms代码研究二

    dedecms代码研究(2)从index开始现在继续,今天讲的主要是dedecms的入口代码.先打开index.PHP看看里面是什么吧.打开根目录下的index.php嗯映入眼帘的是一个if语句.检查 ...

  2. Ibatis 测试出SQL

    String sql = Brg.Global.Map.BaseBatis.GetRuntimeSql("select_T_JewelleryProductType", _Mode ...

  3. Libevent库 编译与使用

    Libevent官网:http://libevent.org/ windows 7下编译: 编译环境: windows 7 + VS2010 (1)解压libevent到F:\libevent\lib ...

  4. 9.S5PV210的时钟系统

    1.时钟域:MSYS.DSYS.PSYS(1)因为S5PV210的时钟体系比较复杂,内部外设模块太多,因此把整个内部的时钟划分为3大块,叫做3个域.(2)MSYS: CPU(Cortex-A8内核). ...

  5. DescriptionResourcePathLocationType Dynamic Web Module 3.0 requires Java

    先保证ide的所有jdk都在1.6及以上,如果还是错就试试下面的 在<build></build>中添加 <plugins> <plugin> < ...

  6. swift 获取控件位置 大小

    var SearchBtn = uibutton() SearchBtn.frame.origin.x   //获取坐标x SearchBtn.frame.origin.Y  // 获取坐标Y Sea ...

  7. 安装mysql5.5时候的报错解决办法:

    每次安装mysql5.5的时候总会报出一下错误: -- Could NOT find OpenSSL (missing: OPENSSL_LIBRARIES OPENSSL_INCLUDE_DIR) ...

  8. 根据关键词kill进程

    #!/bin/sh pid=`ps -ef | grep /usr/bin/memcached | grep -v grep | awk '{print $2}'` kill $pid

  9. Linux系统常见的压缩与打包

    1.gzip, zcat [root@linux ~]# gzip [-cdt#] 檔名参数: -c  :将压缩的数据输出到屏幕上,可透过数据流重导向来处理: -d  :解压缩的参数: -t  :可以 ...

  10. libcurl多线程超时设置不安全(转)

    from http://www.cnblogs.com/kex1n/p/4135263.html (1), 超时(timeout) libcurl 是 一个很不错的库,支持http,ftp等很多的协议 ...