We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

[LEMP] Namespaces of custom classes doesn't work

Environment

Ubuntu 14.04

Phalcon 2.0.7 (manually compiled/installed from repository)

LEMP

Error

Fatal error: Class 'AVCommon\Library\Site\Guest' not found in /home/photochuk/www/photochuk.com/app/common/config/services.php on line 111

 2015/08/29 00:17:32 [error] 9865#0: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Class 'AVCommon\Library\Site\Guest' not found in /home/MY_USER/www/DOMAIN/app/common/config/services.php on line 111" while reading response header from upstream, client: MY_IP, server: www.DOMAIN.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "DOMAIN.com"

Services.php file


<?php
use \Phalcon\DI\FactoryDefault,
    \Phalcon\Mvc\Router,
    \Phalcon\Mvc\Url,
    \Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter,
    \Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter,
    \Phalcon\Mvc\Model\Manager as ModelsManager,
    \Phalcon\Crypt,
    \Phalcon\Http\Response\Cookies,
    \Phalcon\Session\Adapter\Files as SessionAdapter,
    \Phalcon\Flash\Direct as Flash,
    \Phalcon\Flash\Session as FlashSession,
    /* more namespaces */
    \AVCommon\Library\Site\Guest,   //<- namespace, that causes error;
    \UAParser\Parser as UAParser;

$di = new FactoryDefault();

/* more services */

$di->set('guest', function () {
    return new Guest();    // <- Error, line 111
});

/app/common/library/site/Guest.php file


<?php

namespace AVCommon\Library\Site;

use \Phalcon\Mvc\User\Component,
    \AVCommon\Models\Settings;

class Guest extends Component {

//some code

}

My nginx config file


server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    set $root_path '/home/MY_USER/www/DOMAIN/public';
    root $root_path;

    index index.php index.html index.htm;

    server_name www.DOMAIN.com DOMAIN.com;

    try_files $uri $uri/ @rewrite;

    location @rewrite {
        rewrite ^(.*)$ /index.php?_url=$1;
    }

    location ~ \.php$ {
        # try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index /index.php;

        include /etc/nginx/fastcgi_params;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;

       # fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }

    location ~ /\.ht {
        deny all;
    }
}

On a local environment everything works fine. My error resembles the following case

Could anyone please describe the solution in details? I tried to compile phalcon from sources and to install from a repository but the result didn't change. (Sorry for my bad english, that's not my native language).

I would be gratefull for any help.



77.7k
Accepted
answer
edited Aug '15

My hunch is that your local environment is windows, which is case-insesitive so the lowercase directory names doesn't matter. Try matching the directories to the actual namespace casings from point of origin, for eg: /app/common/Library/Site/Guest.php

My hunch is that your local environment is windows, which is case-insesitive so the lowercase directory names doesn't matter. Try matching the directories to the actual namespace casings from point of origin, for eg: /app/common/Library/Site/Guest.php

Yes, you were right ... The problem was in the lowercase names of the directories. Shame on me. Thanks for your quick answer!