Phalcon访问服务的方式

首页index引导文件服务注册


$di = new Phalcon\DI();

$di->set('storage', function(){
return new Storage('/some/directory');
}, true);


内部控制文件访问服务





class FilesController extends \Phalcon\Mvc\Controller
{

public function saveAction()
{

//Injecting the service by just accessing the property with the same name
$this->storage->save('/some/file');

//Accessing the service from the DI
$this->di->get('storage')->save('/some/file');

//Another way to access the service using the magic getter
$this->di->getStorage()->save('/some/file');

//Another way to access the service using the magic getter
$this->getDi()->getStorage()->save('/some/file');

//数组访问方式
$storage = $di['storage'];
}

}