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.

Phalcon session error

well for some reason when i have updated o the new version of phalcon at this time 4.0 , it is throwing me a weird error wich i can not find a solution for since before 4.0 was everything working fine.


Fatal error: Uncaught Error: Class 'Phalcon\Session\Adapter\Files' not found in C:\xampp\htdocs\config\services.php:44 ```

and my  services.php is :

```php 
<?php

use Phalcon\Mvc\Dispatcher\Exception as DispatchException,
    Phalcon\Session\Adapter\Files as SessionAdapter,
    Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter,
    Phalcon\Mvc\Dispatcher as PhDispatcher,
    Phalcon\Events\Manager as EventsManager,
    Phalcon\Mvc\Url as UrlResolver,
    Phalcon\Config\Adapter\Ini,
    Phalcon\DI\FactoryDefault,
    Phalcon\Mvc\Router,
    Phalcon\Dispatcher;

use Phalcon\Mvc\Collection\Manager,
    Phalcon\Db\Adapter\MongoDB\Client;

use Mustache_Engine as Mustache;

$di = new FactoryDefault();
$cf = (Object)json_decode(file_get_contents( __DIR__ . "/../config/config.json"));

$di['db'] = function() use ($cf) {

    return new DbAdapter([
      "host"      => $cf->database->host,
      "username"  => $cf->database->username,
      "password"  => $cf->database->password,
      "dbname"    => $cf->database->dbname,
      "charset"   => $cf->database->charset
    ]);

};

#    The URL component is used to generate all kinds of URLs in the application
$di['url'] = function () {
    $url = new UrlResolver();
    $url->setBaseUri('/');

    return $url;
};

#   Starts the session the first time some component requests the session service
$di['session'] = function (){
    $session = new SessionAdapter();
    $session->start();

    return $session;
};

$di['cookies'] = function() {
    $cookies = new Phalcon\Http\Response\Cookies();
    $cookies->useEncryption(true);
    return $cookies;
};

$di['crypt'] = function() {
    $crypt = new Phalcon\Crypt();
    $crypt->setKey('RWMt1Gz8OEDb');
    return $crypt;
};

$di['configuration'] = function () use ($cf) {
    return (object)[
      "database"      => $cf->database ,
      "mail"          => $cf->mail,
      "debug"         => $cf->server->debug,
    ];
};

#   Handles 404
$di['dispatcher'] = function () {
    $eventsManager = new EventsManager();
    $eventsManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) {

      if ($exception instanceof DispatchException) {
        $dispatcher->forward(array(
          'controller' => 'Error',
          'action'     => 'NotFound'
        ));
        return false;
      }

    });

    $dispatcher = new PhDispatcher();
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
};

#   Configure PHPMailer ( loaded by composer ) , returning the mail ini config & PHPMailer functions
$di['mail'] = function () use ($cf) {
    $mail = new PHPMailer;

    $mail->isSMTP();
    $mail->isHTML(true);

    $mail->CharSet      = $cf->mail->charset;
    $mail->Host         = $cf->mail->host;
    $mail->SMTPAuth     = true;
    $mail->Username     = $cf->mail->username;
    $mail->Password     = $cf->mail->password;
    $mail->SMTPSecure   = $cf->mail->security;
    $mail->Port         = $cf->mail->port;

    #   Pass as object only MAIL config and PHPMailer Functions.
    return (object)[
      "name" => $cf->mail->name ,
      "email" => $cf->mail->email ,
      "functions" => $mail
    ];
};

#    Registering a router
$di['router'] = function () {
    $router = new Router();

    $router->setDefaultModule('Website');
    $router->setDefaultNamespace('Website\Controllers');
    $router->removeExtraSlashes(true);

    require( __DIR__ . "/routes.php" );

    return $router;
};


3.9k
Accepted
answer

From upgrade section from Phalcon docs:

Removed Phalcon\Session\Adapter\Files - replaced by Phalcon\Session\Adapter\Stream

You need make some changes: https://docs.phalcon.io/4.0/en/session

4.0 is a major upgrade and there are some really significant changes you have to make to migrate to that release. Have a look at https://docs.phalcon.io/4.0/en/upgrade for some of the things that need to be changed.

For the new session code, I used the following:

$di->set('session', function() use ($di) {
    $session = new Phalcon\Session\Manager();
    $files = new Phalcon\Session\Adapter\Stream();
    $session->setAdapter($files)->start();
    return $session;
});