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. [清理页面缓存]asp.net、html

    (1)   MVC BaseController: Controller内 protected override void Initialize(System.Web.Routing.RequestC ...

  2. windows无提示关闭页面

    今天碰到个问题,需要自动关闭网页,网上找了方法,一直在火狐测试,一直没反应,还以为写错了,后来发现用火狐需要进行设置(后文有提供方法),IE可正常使用... 下面提供部分代码: 需要自动关闭网页,可以 ...

  3. css之首字母大写 | 全部大写 | 全部小写 | text-transform

    div{text-transform:capitalize}首字母大写

  4. 安装 ppsycopg2报错, Error: You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application

    sudo apt-get install libpq-dev

  5. 今天遇到的关于mysql的max_allowed_packet的问题

    今天,运维组的同学来找我,说是备份池的文件描述没有显示出来,而且是从20号开始就不能显示,之前的文件描述就能显示,而且20号他们上传备份的数据确实是传过来的.但是是在web界面文件描述显示不出来. 先 ...

  6. 8.8 CSS知识点1

    什么是CSS CSS(Cascading Style Sheet) 层叠样式表 CSS3在CSS2的基础上增加了很多强大的新功能,目前主流浏览器都支持CSS3大部分功能.为了更好的向前兼容,不同的浏览 ...

  7. 3.Mybatis全局配置文件属性详解(SqlMapConfig.xml)

    首先我们要知道一点,该配置文件的中属性的存放是有顺序的,没有办法随意的乱放.如果你属性的配置位置出错,会有如下错误提示: The content of element type "confi ...

  8. position:absolute、float、display:inline-block 区别

    position: absolute会导致元素脱离文档流,被定位的元素等于在文档中不占据任何位置,在另一个层呈现,可以设置z-index.PS的图层效果就是position: absolute. fl ...

  9. mysql - 最小缺失值查询

    初始化数据 DROP TABLE IF EXISTS X; CREATE TABLE X( a INT UNSIGNED PRIMARY KEY, b ) NOT NULL )ENGINE=INNOD ...

  10. 在线学习体验大PK 云智慧发布在线教育网站性能监测报告

    互联网不但改变了我们的生活.娱乐和消费方式,也推动各行各业进行着快速变革,越来越多的职场人士必须通过不断的学习.充电才能跟上行业发展的步伐,获得职业的提升,而这也引发了国内教育市场的爆炸式发展.据统计 ...