How to find www dir path inside presenter?
- orlov0562
- Member | 2
Hi there,
I have created dir www/uploads and want to store images inside it, so I need to get full path to this directory.
I have found wwwDir inside Configurator::getDefaultParameters(), but I haven't found way to get it.
Yes, I can do something like define(‘WWW_DIR’, dirname(__FILE__)); inside www/index.php (or bootstrap.php), but I think it's a bad way.
So, what is the best way to find (or retrieve) www dir path inside presenter?
Last edited by orlov0562 (2015-08-28 13:31)
- orlov0562
- Member | 2
Thank you very much! You are my hero, it is exactly what I was looking for.
I have also described for other visitors how they can use that solution:
1] Create dir app/services
2] Create service class app/services/WebDir.php with next
contents:
<?php
namespace App\Services;
class WebDir {
private $wwwDir;
public function __construct($wwwDir) {
$this->wwwDir = $wwwDir;
}
public function getPath($fromBaseDir=''){
return $this->wwwDir.DIRECTORY_SEPARATOR.$fromBaseDir;
}
}
3] Declare this service in app/config/config.neon
services:
...
webDir: App\Services\WebDir(%wwwDir%)
4] Inject this servics in your presenter, ex: app/presenters/PostPresenter.php
class PostPresenter extends BasePresenter
{
private $webDir;
public function injectWebDir(Services\WebDir $webDir)
{
$this->webDir = $webDir;
}
...
}
5] And finally retrieve www dir in your action:
$imageDir = $this->webDir->getPath('uploads/images/');
// this code will return full path to the images upload directory on the server
// smth like: /home/user/public_html/site.com/www/uploads/images/
Last edited by orlov0562 (2015-08-28 22:57)
- Honza Kuchař
- Member | 1662
Thanks to the fact that Presenter
is also service and is
initialized the same way as other services in DI container. So you can provide
any service or configuration option from DI.