2016-05-14 142 views
1

我想發送帶附件的電子郵件,所以我需要PDF文件的絕對路徑。如何獲取Symfony2控制器中文件的絕對路徑?

我試圖在控制流動代碼:

$this->get('templating.helper.assets')->getUrl('MyDoc.pdf', 'doc'); 

但這返回:

/bundles/MyBundle/doc/MyDoc.pdf?v=1 

這是一個沒有用於Web訪問的域的路徑。但是我需要啓動磁盤根目錄的真正路徑,如下例所示。這怎麼可能?謝謝。

/home/MyAccount/public_html/web/src/company/MyBundle/Resources/public/doc/MyDoc.pdf 

回答

3

$this->get('templating.helper.assets')資產助手是約HTTP URI,沒有目錄路徑。 然而,你可以很容易得到這樣的權利絕對目錄的網絡路徑:

$appPath = $this->container->getParameter('kernel.root_dir'); 
$webPath = realpath($appPath . '/../web'); 

$webPath應該返回是這樣的:

/home/martins/www/symfony-project/web 
1

作弊表爲別人的位尋找不同的URL /不同應用程序的URI路徑。

使用Symfony\Component\HttpFoundation\Request組件,有很多不同的方法可以調用此方法。

考慮這個鏈接http://dev.col.com/app_dev.php/my-route?bar=1&foo=bar

$r = $this->getRequest(); 
$r->getClientIp()     127.0.0.1 
$r->getScriptName()     /app_dev.php 
$r->getPathInfo()     /my-route 
$r->getBasePath()     '' 
$r->getBaseUrl()     /app_dev.php 
$r->getScheme()      http 
$r->getPort()      80 
$r->getHttpHost()     dev.col.com 
$r->getRequestUri()     /app_dev.php/my-route?bar=1&foo=bar 
$r->getBaseServerUrl()    http://dev.col.com 
$r->getUri()      http://dev.col.com/app_dev.php/my-route?bar=1&foo=bar 
$r->getUriForPath("/other-path") http://dev.col.com/app_dev.php/other-path 
$r->getQueryString()    bar=1&foo=bar 
$r->isSecure()      false 
$r->getHost()      dev.col.com 
$r->getMethod()      GET 
$r->isXmlHttpRequest()    false 

或者你也可以做到這一點在枝條爲好。例如:

<div> 
    <a href="{{ job.url }}"> 
     <img src="{{ app.request.scheme ~ '://' ~ app.request.host }}{{ asset(job.webPath) }}" alt="{{ job.company }} logo" /> 
    </a> 
</div> 

<div> 
    <a href="{{ job.url }}"> 
     <img src="{{ app.baseServerUrl }}{{ asset(job.webPath) }}"alt="{{ job.company }} logo" /> 
    </a> 
</div>