Symfony2 KnpLabsGaufrette - Get local filesystem per twig (downloadable files) - php

i'm using this config.yml.
knp_gaufrette:
adapters:
uploaded_files:
local:
directory: "%kernel.root_dir%/../web/uploads"
create: true
filesystems:
uploaded_files:
adapter: uploaded_files
alias: uploaded_files
Now i want to access uploaded files per twig.
Also for example:
<a href="{{ path('gaufrette_download', {system: 'uploaded_files', file: 'test.txt'}) }}>{{ 'Download' | trans }}</a>
The file should have a path like...
http://localhost/web/uploads/test.txt
I want a direct access to the file(s).
No controller (action).
Is this possible? Any ideas?

If your folder is web-accessible (i.e. you can type the url http://localhost/web/uploads/test.txt in your address bar and download the file), all you have to do is map the route gaufrette_download to that path. Your bundle's routing.yml could look like this (notice the missing defaults: { controller: ... }):
gaufrette_download:
path: /web/uploads/{file}
If your .htaccess is defined properly your web server should serve the file instead of accessing your application. You might have to add requirements for file, e.g. to allow for slashes (search the symfony cookbook for this)
If you just want to skip writing a controller (action), you could just as well create an event listener which is triggered when your request matches the route.

Related

What Does the `type:` Configuration in a Symfony Route Configuration Control?

What does the type: configuration in a Symfony routing file control? What are its valid values?
I can't find this configuration field explicitly documented anywhere. It's referenced indirectly in Symfony's routing documentation.
app_directory:
resource: '../legacy/routing/'
type: directory
and seems related to loading in additional routes. However, its behavior (or all its allowed values) doesn't seem to be explicatly defined anywhere. I can make a guess that it somehow tells Symfony how to load the external routes, but I'd love to know
Is my guess correct?
Are there valid values other than directory or annotation?
Is this formally documented anywhere?
Is there a spot in the Symfony internals that would be a good place to start finding these answers for myself?
You can find how the type works in the Symfony documentation, see code below. It controls if the routes should be loaded from the PHP annotations or the YAML or XML files found in that (bundle) directory.
app_file:
# loads routes from the given routing file stored in some bundle
resource: '#AcmeOtherBundle/Resources/config/routing.yaml'
app_annotations:
# loads routes from the PHP annotations of the controllers found in that directory
resource: '../src/Controller/'
type: annotation
app_directory:
# loads routes from the YAML or XML files found in that directory
resource: '../legacy/routing/'
type: directory
app_bundle:
# loads routes from the YAML or XML files found in some bundle directory
resource: '#AppBundle/Resources/config/routing/public/'
type: directory

Symfony Bundle Load YML Configuration

Since I am new to Symfony and I couldn't manage to find some useful information in google I decided to write to you.
I've read about the way of loading custom DI alias information from a dependency injector in your bundle and how to create a Configuration class that will expose the alias structure. However I am to some extend confused how I can create a file, for example routing.yml, in my AcmeBundle/Resources/config/ folder and read the data from it. E.g:
some_alias:
resource: "#AcmeBundle/Controller/"
type: annotation
prefix: /
I want to make a bundle with routing, independent from the main configuration files in the app folder.
You can create your bundle routing.yml in your WhateverBundle/Resources/config/routing.yml and the in the app/routing.yml just include your bundle's routes.
mybundleorwhatever:
resource: "#WhateverBundle/Resources/config/routing.xml"

Symfony2 LiipImagineBundle path cache probleme

I've setup a LiipImagineBundle configuration on a linux computer (xubuntu 14.10) :
routing.yml
_liip_imagine:
resource: "#LiipImagineBundle/Resources/config/routing.xml"
config.yml
liip_imagine:
resolvers:
default:
web_path: ~
filter_sets:
cache: ~
dashboard_thumb:
quality: 75
filters:
thumbnail: { size: [60, 60], mode: outbound }
and in my twig template :
<img src="{{ asset(company.logo.getPath) | imagine_filter('dashboard_thumb') }}">
All sources images are under web/uploads path
This was working fine, image thumbnails are generated under web/media/cache/dashboard_thumb/uploads/
My source files are stored under an USB stick, and i lanch server with server:run commande (so under 127.0.0.1:8000)
But recently, i lanched the server under another computer (linux mint 17) and then, image cache are not generated anymore.
when i look at the generated html source, file path for images are :
http://127.0.0.1:8000/media/cache/resolve/dashboard_thumb/uploads/myimage.png
so i dont know why there is a 'resolve' in the path
Other thing, if i launch the command :
liip:imagine:cache:resolve uploads/myimage.png
the path and image web/media/cache/dashboard_thumb/uploads/myimage.png are well created
why this doesnt work automatically?
Thanks.
Seem a problem about Setting up Permissions. Basically the System operating users for the CLI(and deploy) and the web server must be on the same group.
Check the doc for Symfony Application Configuration and Setup
PS: the command you are looking for is chown but is only a workaround an i suggest you to fix to operating user layer.
Hope this help
... so i dont know why there is a 'resolve' in the path
If You do not have a cache for your image, LiipImagineBundle (imagine_filter in your case) generates a route according this rule
liip_imagine_filter:
path: /media/cache/resolve/{filter}/{path}
defaults:
_controller: '%liip_imagine.controller.filter_action%'
methods:
- GET
requirements:
filter: '[A-z0-9_-]*'
path: .+
, and your request handles by ImagineController https://github.com/liip/LiipImagineBundle/blob/1.0/Controller/ImagineController.php
So, You see not image path, but route. Controller generates the cache and your second request to this image will give you actually path to image.
There is a problem, if your need to attach image to mail message, You have to resolve image before attaching this one.
Also if cache not generates anymore, the problem maybe in your web server configuration. Imagine that Your Nginx decides that web/media/cache/* is static content, so, the route web/media/cache/resolve just not working.

Access external directories from symfony2

I have images that are stored in a directory in /var/lib/myproject. So they are outside of my symfony project. I would like to display them in a twig template or via css.
How could I access them as symfony is catching every url and rewrites them?
Setting a specific route doesn't work :
my_project_res:
resource: "/var/lib/myproject"
prefix: /res

prefixing asset path for files in Symfony2 and Twig

I upoad my files to web/files/images/ and i'm trying to link them using asset function:
<img src='{{ asset(article.image) }}'/>
but this produces URLs like /img1.jpg
I need to prefix (setting a base folder) asset URLs to force it to make /files/images/img1.jpg
How can I prefix asset urls?
While you could easily introduce a new twig variable for that purpose (i.e. in your base template) ...
{% set asset_base = '//files/images' %}
... or create a static global twig variable ...
# app/config/config.yml
twig:
globals:
asset_base: //files/images
... and afterwards use it inside i.e. the src attribute of your img tag ...
<img src='{{ asset_base }}{{ asset(article.image) }}'/>
... symfony2 already provides this functionality in form of the assets_base_urls directive:
# app/config/config.yml
framework:
templating:
assets_base_urls:
http: [http://domain/files/images]
ssl: [https://domain/files/images]
You can aswell set the base urls both at once by providing a protocol-relative url:
framework:
templating:
assets_base_urls: //files/images
More information about the directive can be found in the documentation chapter FrameworkBundle Configuration#assets-base-urls.
Note that since symfony 2.7 the assets_base_urls solution from #nifr will only work with valid urls, not with relative pathes.
See here https://github.com/symfony/symfony/issues/14332
The solution for > 2.7 is to use the new assets component for relative base path as prefix.
framework:
assets:
base_path: 'assets'
If you don't want to prefix all assets, the asset helper provides another really nice way to do this using packages.
Packages have a name and can have other settings, such as the version and the base path. You configure packages in the configuration and you can set the name of the package to use in the second argument of the asset function.
In your case:
framework:
templating:
packages:
images:
base_url: /web/files/images # can also be scheme specific
And then in you template:
<img src="{{ asset('...', 'images'). }}">
More information: http://symfony.com/doc/current/components/templating/helpers/assetshelper.html#multiple-packages

Categories