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

create URL

Everybody, excuse me how can I create a and dirname (FILE) path?Now I'm in the bootstrap WWW_HOST and then set the constants in the program $file - > moveTo (WWW_HOST. 'upload/img);But I think it is not a good solution, and my style is used in the relative path, sometimes image path will be a problem, please everybody a good solution



58.4k

Hey

Do you want save images on server container you app or save cnd server ?

In your DI setup, you could set the URL properties like this:

$di->set('url', function() {
    $url = new \Phalcon\Mvc\Url();
    // my local dev machine has no virtual domains, just localhost/project-a. localhost/project-b etc
    $url->setBaseUri('/fooProject/public_html/');
    $url->setBasePath('/home/me/fooProject/');
    return $url;
});

In controller or view (I don't know the volt style), use it like this:

// url
echo $this->url->get('bar/baz'); // /fooProject/public_html/bar/baz
echo $this->url->get('bar/baz', ['id'=>'2', 'apple' => 'pie'], true)); // /fooProject/public_html/bar/baz?id=2&apple=pie
// path
echo $this->url->path('path/to/my/files/hat.jpg'); // /home/me/fooProject/path/to/my/files/hat.jpg

If you don't want the BaseUri prefixed to your base uri, set the 3rd param of get() to false. The path methods are also in this page: https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Url.html