XF 2.0 How do you get an attachment's URL?

Jaxel

Well-known member
I know I can get an attachment's thumbnail using:
Code:
<img src="{$attachment.thumbnail_url}" alt="" />

But how would I get the full attachment URL? Hopefully, ignoring attachment view permissions.
 
Attachments are only accessible via the attachments route. If you want to bypass permissions you’ll have to serve it from your own route/controller.
 
This is what I do:

PHP:
$baseUrl = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";						
$attachmentUrl = $baseUrl . \XF::app()->router()->buildLink('attachments', $attachment[$i]);
 
@AndyB

I’m not near a development environment, but can’t you use canonical and leave off the baseUrl?

Code:
$attachmentUrl = $baseUrl . \XF::app()->router()->buildLink('attachments', $attachment[$i]);

Could be

Code:
$attachmentUrl = \XF::app()->router()->buildLink('canonical:attachments', $attachment[$i]);

Regardless, @Chris D stated bypassing permissions would require a custom route or controller. Is that the purpose of the baseUrl?
 
I have no idea where to even get started in this... the attachment system is a Gordian Knot to me...
 
can’t you use canonical and leave off the baseUrl?

Thank you, LPH. Tested works perfectly, I will update my add-on.

PHP:
$attachmentUrl = \XF::app()->router()->buildLink('canonical:attachments', $attachment[$i]);
 
  • Like
Reactions: LPH
Well I found my answer:
Code:
{{ link('full:attachments', $attachment, {'hash': $attachment.temp_hash}) }}
 
  • Like
Reactions: LPH
You right... I ended up just making my own controller:

Code:
<?php

namespace EWR\Porta\Pub\Controller;

use XF\Mvc\ParameterBag;

class Attachment extends \XF\Pub\Controller\AbstractController
{
    public function actionIndex(ParameterBag $params)
    {
        $attachment = $this->em()->find('XF:Attachment', $params->attachment_id);
        
        if (!$attachment)
        {
            throw $this->exception($this->notFound());
        }

        return $this->plugin('XF:Attachment')->displayAttachment($attachment);
    }
}
 
This is what I do:

PHP:
$baseUrl = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";                       
$attachmentUrl = $baseUrl . \XF::app()->router()->buildLink('attachments', $attachment[$i]);
Great ! Thanks
 
Back
Top Bottom