Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Where to write common functions/variables

I am very new to phalcon and I have to use some common variables or functions in the whole project then where I have to write these? For example I have to use phalcon logging and I have written the following code in public/index.php file as:

use Phalcon\Mvc\Controller; use Phalcon\Logger\Adapter\File as FileAdapter; $logger = new FileAdapter("../app/logs/test-".date("Y-m-d").".log", array('mode' => 'w'));

But when I use $logger->log("Test"); this in controller then it gave me an error of undefined variable.

Hence please give me a proper flow so that I can write common things and used them into my whole project.



11.3k
Accepted
answer
edited May '15

Ideally you set all your services on the Di/Service Locator so they are accesible where ever the Di is inyected (Controllers, Plugins, etc), also you can get the default Di by using Di::getDefault() and get the Di like a singleton.

check out the http://docs.phalconphp.com/en/latest/reference/di.html and read it well, using it could lead you to a good way of solve common problems through services.

P.D. also, if you are doing OOP try to encapsulate the responsabilities of each object. This is, if you want to log something try to do it out of the class that does the action, the loggin responsability must be in its own class. You can check the observer pattern, or use the eventsManager from phalcon... doing it this way you avoid multiple dependencies between classes.

Yo can see an example here:

https://github.com/phalcon/mvc/blob/master/simple-volt/app/config/services.php

the app define services to live on the Di, then on the controllers (thanks to some magic from phalcon inyectable interface) they can use the services:

http://docs.phalconphp.com/en/latest/reference/controllers.html#injecting-services

edited May '15

Thanks but in service.php I have write this code use \Phalcon\Logger\Adapter\File as LoggerFile;

//Register a service 'logger' with a class name and its parameters $di->set('logger', array( 'className' => 'Phalcon\Logger\Adapter\File', 'arguments' => array( array( 'type' => 'parameter', 'value' => '../apps/logs/error.log' ) ) ));.

Then how can I call it on my controller to set log.

Ideally you set all your services on the Di/Service Locator so they are accesible where ever the Di is inyected (Controllers, Plugins, etc), also you can get the default Di by using Di::getDefault() and get the Di like a singleton.

check out the http://docs.phalconphp.com/en/latest/reference/di.html and read it well, using it could lead you to a good way of solve common problems through services.

P.D. also, if you are doing OOP try to encapsulate the responsabilities of each object. This is, if you want to log something try to do it out of the class that does the action, the loggin responsability must be in its own class. You can check the observer pattern, or use the eventsManager from phalcon... doing it this way you avoid multiple dependencies between classes.

You'd use the logger from the di:

$this->getLogger()->log('test');
edited May '15

yep, the controller tries to find a service on the di for whatever non defined property, so been inside a controller you just need to :

    $this->logger->log('this is a message');
    // or
    $this->getDi()->get('logger')->log('this is a message');
    // pr
    Di::getDefault()->get('logger')->log('this is a message');
    // etc... there is more examples on the last link i posted.

Thanks but in service.php I have write this code use \Phalcon\Logger\Adapter\File as LoggerFile;

//Register a service 'logger' with a class name and its parameters $di->set('logger', array( 'className' => 'Phalcon\Logger\Adapter\File', 'arguments' => array( array( 'type' => 'parameter', 'value' => '../apps/logs/error.log' ) ) ));.

Then how can I call it on my controller to set log.

Ideally you set all your services on the Di/Service Locator so they are accesible where ever the Di is inyected (Controllers, Plugins, etc), also you can get the default Di by using Di::getDefault() and get the Di like a singleton.

check out the http://docs.phalconphp.com/en/latest/reference/di.html and read it well, using it could lead you to a good way of solve common problems through services.

P.D. also, if you are doing OOP try to encapsulate the responsabilities of each object. This is, if you want to log something try to do it out of the class that does the action, the loggin responsability must be in its own class. You can check the observer pattern, or use the eventsManager from phalcon... doing it this way you avoid multiple dependencies between classes.