Sunday, February 13, 2022

[FIXED] Cakephp 3.0 ROOT variable gets set wrong

Issue

After a stock install and creation of a basic controller, the environment variables are being set wrong.

For example, ROOT gets set as follows:

/var/www/html/appname/src//var/www/html/appname

In other words, the Apache webserver DocumentRoot is being appended to it.

Why?


Solution

What I learned is that if I specify the absolute path to the file in a controller, the Apache DocumentRoot gets appended to the ROOT variable. For example:

public function getFile() {
    // disable the default layout for the view
    $this->viewBuilder()->layout(false);
    $path = ROOT . '/file/file.txt';
    $this->response->file($path, array('download' => true));
    return $this->response;
}

The browser cannot find the file at location /var/www/html/app/src//var/www/html/app/src/file/file.txt

However the following works as expected:

public function getFile() {
    // disable the default layout for the view
    $this->viewBuilder()->layout(false);
    $path = '/file/file.txt';
    $this->response->file($path, array('download' => true));
    return $this->response;
}

The ROOT location seems to get automatically pre-prended to the path I have set above. I was not aware of this and haven't read anything about it in the documentation, so I am still not sure why it works this way.



Answered By - geofrey rainey

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.