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

application($di)->getContent(); return empty string on Linux

The difference between windows and Linux

####### Code : (new \Phalcon\Mvc\Application($di))->handle()->getContent();

Hi,All!

I have a problem on linux system...

First of all,I have a controller file...

public function getAction()
{
    return "f**k man~~~";
}

In the index.php file...

  echo (new \Phalcon\Mvc\Application($di))->handle()->getContent();

So,output result as follow...

1.on windows(aphache)

"f**k man~~~"

2.but on linux(nginx),the result is a empty string~~~

~~if I modify the controller code...

public function getAction()
{
    echo "f**k man~~~";
}

on the linux,the result is successful!

So,I found,on linux(nginx), the follow code return a empty string!

(new \Phalcon\Mvc\Application($di))->handle()->getContent();


58.4k

Hello

I see your code difference betweent windows and Linux

Hello

I see your code difference betweent windows and Linux Thanks for your help

code in github site: https://github.com/q-phalcon/q-phalcon

you can use composer tool to download this code:

composer create-project q-phalcon/q-phalcon

The code on the two system is exactly the same.

My nginx config is


server {
        listen          12333;
        server_name     localhost;
        root            /home/qvil/www/public;
        error_page 404  /404.html;
        index index.php index.html index.htm;

        location ~* \.(ico|jpg|png|gif|jpeg|swf|flv|zip)$ {
                if (!-e $request_filename) {
                        return 404;
                }
        }

        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:12334;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
    }
}

Thanks again!!

edited Jul '16

Why do you use parenthesis here?

echo (new \Phalcon\Mvc\Application($di))->handle()->getContent();
//try this:
new \Phalcon\Mvc\Application($di)->handle()->getContent();

On success, if your controller returns response it will be flushed automatically to the client. For testing purposes you can still echo it, but the main purpose for getContent() method is to flush output buffers to the client, if any, so echo should be avoided.

The most important - how did you defined your Routes, i.e. Caps?

I have defined my Routers as follow: ( /home/tome/www/public/index.php )


try {
    require_once "../app/Controllers/IndexController.php";

    define('MY_ROOT_PATH','/home/tome/www/');
    define('MY_VIEW_PATH','/home/tome/www/view/');

    $di = new \Phalcon\DI\FactoryDefault();
    $router = new \Phalcon\Mvc\Router();

    $router->setDefaults([
        "namespace" => 'App\Controllers',
        "controller" => 'Index',
        "action" => 'test',
    ]);

    $di->set('url', function () {
        $url = new \Phalcon\Mvc\Url();
        $url->setBaseUri(MY_ROOT_PATH);
        return $url;
    });

    $di->set('view', function () {
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir(MY_VIEW_PATH);
        return $view;
    });

    $app = new \Phalcon\Mvc\Application($di);
    $content = $app->handle()->getContent();

    $response = new \Phalcon\Http\Response();
    $response->setContent($content);
    $response->send();

    } catch (\Exception $ex) {
        echo $ex->getMessage();
}

I had tested this code,under the windwos,it is successful,but,uder the linux,,,also return a empty string!

My controller file as follow. ( /home/tome/www/app/Controllers/IndexController.php )

    namespace App\Controllers;
    use Phalcon\Mvc\Controller;

    class IndexController extends Controller
    {
        public function testAction()
        {
            return "f**k man~~~";
        }
    }
edited Jul '16

So you set your URL component and setBaseUri as an absolute path on a filesystem? How is that suppose to work on a GNU/Linux, you need to supply your public directory for setBaseUri() method.

Check official docs: https://docs.phalcon.io/en/latest/reference/url.html#generating-urls-and-paths

All of this should be in relation with your web server configuration. So if your root path is /full/path/to/phalcon/app/public you only need to set setBaseUri to /.

Also turn on error reporting on Linux to see what else it complains.

Thanks for your help~~ I will use relation path to my web server!

But, the problem of "empty response" ,I haven't come up with a solution ~~~ oh... I was not found any error log for Nginx or Php-fpm...

Hi,My friend~~

I was found a doubt,So this problem has to de solved!

In my web server ( nginx ) , My root directory is '/home/tome/www/',

my public directory is '/home/tome/www/public',

my view directory is '/home/tome/www/app_view'

erenow,my view directory was empty,

now ,I create a file - "/home/tome/www/app_view/Index/test.phtml", the file content as follow:

"hello, my phtml string..."

Like this, the code

 $content = $app->handle()->getContent();

return the view file's content!

edited Jul '16

1st, you should set your nginx to target /public directory at it's absolute path. So your home page (with domain, if any) would become www.domain.com and not www.domain.com/public. Then set your views, controllers etc dirs relative to the root path.

In your index.php set constant:

define('APP_PATH', realpath('..'));
//resolve config object
$config = new \Phalcon\Config\Adapter\Ini(APP_PATH . '/config/config.ini');

Then in your config.ini (or .php) set dirs one level up from current: NOTE: your config file is in config directory, thus resulting your models, controllers etc are one level up:

modelsDir     = ../models/
controllersDir     = ../controllers/
migrationsDir = ../migrations/
viewsDir      = ../views/
baseUri       = /

baseUrl needs to be referred in your services container as:

//URL service
$di->setShared('url', function () use ($config) {
    $url = new UrlResolver(); // alias of: \Phalcon\Mvc\Url
    $url->setBaseUri($config->application->baseUri);
    return $url;
});