Skip to content
cayman edited this page Jun 14, 2016 · 2 revisions
  1. Config is define at conf.json

  2. An typical config

    {
        "key": //class identifier,if no "class" set below, "key" is the class name.
        {
            "class":"class_name", //Optional, class name
            "singleton":true,// Optional, singleton, default is false
            "pass_by_construct":true,//Optional, pass properties by construct, default is false
            "properties":  
            {
                 "name1": "{tpl_key_1}/apis/", //{tpl_key_1} is a template variable, will be replace when IoCFactory load this config.
                 "name2": "value",//set name2 as a constant
                 "name3":"@key1" //set name3 as an instance of "key1"
            }
        },
        "key1":{
            ...
        }
  3. Examples

    Config the class "Users" with conf.json.

    class Users{
    
    /**
     * @property({"default":"@default_db"}) $this->db will be inited as an instance of "default_db" by default.
     * @var PDO
     */
    public $db;
    
    /**
     * @property the dir for storing avatar files.
     */
    public $avatar_files;
    }

    And its config

    {
    "Users":{
        "properties": {
            "avatar_files": "/var/avatar_files/"
         }
    },
    "default_db":{
    "singleton":true, //Optional,we use singleton to share default_db in all requests.
    	"class":"PDO",    //Optional
    	"pass_by_construct":true, //Optional
    	"properties":{
            "dsn":"mysql:host=127.0.0.1;port=3306;dbname=test;charset=UTF8",
            "username":"test",
            "passwd":"test"
        }
    }
    }

    phprs create Users with the config, if no "Users" config, phprs use new Users() to create "Users". Here we config it, so:

    1. phprs create "Users" with ReflectionClass::newInstanceWithoutConstructor(__construct not called),
    2. set avatar_files
    3. call __construct In this sample,$db is specified not in Users's config, so the default value @default_db is used, and '@' means it an instance.
  4. Config phprs

    {
    "phprs\\Router": {
        "properties": {
            "export_apis":true //Enable output api documents, if true, visit http://your-host:port/apis/ to get documents
            //,"hooks":["MyHook1","MyHook2"] //Enable hooks
            //,"url_begin":1 // if url is "/abc/123", then 1st path node "/abc/" will be ignored.
        } 
    },
    "phprs\\Invoker": {
        "properties": {
            //"cache":"@invokeCache"  //cache result of apis(the apis with @cache)
        } 
    }
    /** if the property "cache" of phprs\\Invoker is set
    ,
    "invokeCache":{
    	"class":"phprs\\util\\RedisCache",
    	"singleton":true,
    	"properties":{
    		"serialize":true,
    		"host":"127.0.0.1",
    		"port":"6379"
    	}
    }*/
    
    /** 
    //db config sample, and ezphp(https://github.com/caoym/ezsql) can work with PDO simply
    ,"db":{
    	"singleton":true,
    	"class":"PDO",
    	"pass_by_construct":true,
    	"properties":{
    		"dsn":"mysql:host=127.0.0.1;dbname=xxxxxx;charset=UTF8",
    		"username":"xxxxxx",
    		"passwd":"xxxxxx"  		
    	}
    }*/

}

 ```
Clone this wiki locally