create URL

Everybody, excuse me how can I create a and dirname (FILE) path?Now I'm in the bootstrap WWWHOST and then set the constants in the program $file - > moveTo (WWWHOST. '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



33.1k

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: http://docs.phalconphp.com/en/latest/api/Phalcon%5Mvc%5Url.html