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. 2016-08-16: copy-and-swap

    #include <algorithm> // std::copy #include <cstddef> // std::size_t #include <stdio.h ...

  2. swift 存储属性和计算属性 set{}和get{} didSet{}和willSet{}

    首先我们看一段代码: var A:Int = 0 var B:Int = 0 var C:Int { get { return 1 } set { print("Recived new va ...

  3. JS 退出系统并跳转到登录界面的实现代码

    js代码如下: <script language="javascript" type="text/javascript"> function log ...

  4. 15 个有用的 MySQL/MariaDB 性能调整和优化技巧(转载的一篇好文)

    MySQL 是一个强大的开源关系数据库管理系统(简称 RDBMS).它发布于 1995 年(20年前).它采用结构化查询语言(SQL),这可能是数据库内容管理中最流行的选择.最新的 MySQL 版本是 ...

  5. POJ(3468)

    A Simple Problem with Integers http://poj.org/problem?id=3468 #include <stdio.h> struct node { ...

  6. win8自动升级win8.1后 wampserver无法启动

    原因是升级时win8把其他的系统服务都给停止了. 解决办法是左键点击wamp的小图标,选择apache/mysql - service - 安装服务. 然后再选择启动服务,即可.

  7. XproerIM V1,2,12,65376 发布。

    客户端下载:http://yunpan.cn/QTCxKvcpC4Iet  访问密码 9141 更新说明:1.增加表情功能. 更新代码截图:

  8. Android菜鸟成长记1--环境的搭配和第一个项目的构建

    一.配置Android环境 1.下载JavaJDK的本地,然后拷贝出来(因为Android实在java的基础上开发的,所以要先配置java环境) 2.java环境变量的配置 配置方法(我的电脑上-&g ...

  9. js读取解析JSON数据

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...

  10. [转]libsvm 训练后的模型参数讲解

    http://blog.sina.com.cn/s/blog_6646924501018fqc.html 主要就是讲解利用libsvm-mat工具箱建立分类(回归模型)后,得到的模型model里面参数 ...