We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Any Way to access session variable of a logged user in config.php

Hi, i am new to phalcon and is now developing a simple project using phalcon, so my question is " is there any way to access the session identity variable of a logged in user in the config.php file if so, can you please explain me how? Pls help me.

The real thing i want is that i have included dhtmlx scheduler in my project and i want to display the events based on the current user company-id and the connector file for the scheduler is located in public folder and in that connector file only the query has been written to fetch events from the events table.

THis is my scheculer.php connector file and i want the $comid to be the current logged in user company-id. Any ways?? pls help me.


use Phalcon\Config; 

require __DIR__ . "/../../app/config/config.php";

$config = new Config($settings);

$username = $config->database->username;

$host = $config->database->host;

$password = $config->database->password;

$dbname = $config->database->dbname;

$dbadapter = $config->database->adapter;

$comid = $config->companyId;



$dbtype = $dbadapter;

$scheduler = new schedulerConnector($res, $dbtype);

$scheduler->render_sql("SELECT * FROM engagement WHERE eng_companyid = $comid","id","fromdate,todate,title,description,place,location,status"); 

The session service is only dependant on a DependencyInjector, so you could create a $di and require_once your services.php, but why don't you wrap it in a controller, as Phalcon is designed for?

Register 3rd party library

// loader.php

Create a config service you so can access it later on

// services.php
$di->setShared('config', function() use($config) {
    return $config;

Set up an action which only renders the action part, I assume schedulerConnector::render_sql prints out JSON/HTML.

// SchedulerController.php
public function eventsAction() {
        View::LEVEL_MAIN_LAYOUT     => true,
        View::LEVEL_LAYOUT          => true,
        View::LEVEL_BEFORE_TEMPLATE => true,
        View::LEVEL_AFTER_TEMPLATE  => true,
   $cfg = &$this->config->database;
   $scheduler = new schedulerConnector($res, $cfg->adapter);
   $comid = $this->session->get('user_company_id');
   $scheduler->render_sql("SELECT * FROM engagement WHERE eng_companyid = $comid","id","fromdate,todate,title,description,place,location,status");

Show result in the view

// scheduler/events.volt

{{ content() }}