Answers.Body: <p>You could create a simple plugin with <a href="https://pluginfactory.io/" rel="nofollow noreferrer">Pluginfactory</a> and copy the two function I'll give you to create the URL you like</p>
  
  <ol>
  <li>Go to the page, turn on the light switch for "Controller" but leave the name blank, download it, copy it to <code>craft/app/plugins/</code></li>
  <li><p>Include this function to your plugins main file. If you did not disable preview/example code in the page you'll see there is already a <code>cocktails</code> route there</p>
  
  <pre><code>public function registerSiteRoutes()
  {
      return [
          'menu' =&gt; ['action' =&gt; '&gt;&gt;insertYourPluginNameHere&lt;&lt;/downloadMenuCard'],
      ];
  }
  </code></pre></li>
  <li><p>in this case <code>www.exampleurl.de/menu</code> would download your file, you can change the key to whatever you want.</p></li>
  <li><p>Insert this function in your created Controller</p>
  
  <pre><code>public function actionDownloadMenuCard()
  {
      $globalSetHandle = 'menuCard'; // change the handle of your global set here if needed
      $fieldHandle = 'menuCard'; // change the handle of your field here if needed
      $globals = craft()-&gt;globals-&gt;getSetByHandle($globalSetHandle);
      /** @var AssetFileModel $asset*/
      $asset = $globals-&gt;$fieldHandle-&gt;first(); 
      $source = $asset-&gt;getSource();
      $sourcePath = $source-&gt;settings['path'];
      $folderPath = $asset-&gt;getFolder()-&gt;path;
  
      $assetFilePath = $sourcePath.$folderPath.$asset-&gt;filename;
  
      craft()-&gt;request-&gt;sendFile($assetFilePath, IOHelper::getFileContents($assetFilePath));
  }
  </code></pre></li>
  </ol>
  
  <p>You can include a redirect after that if you want in order to display the landing page or whatever you want. If you don't want to download the file at all and just display it you can do <code>craft()-&gt;request-&gt;redirect($asset-&gt;getUrl());</code> after you fetched the asset</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Another approach without a plugin would be to add the route in your <code>craft/config/routes.php</code> file</p>
  
  <pre><code>return array(
      'menu' =&gt; 'download.twig',
  );
  </code></pre>
  
  <p>And do the redirect in the template</p>
  
  <pre><code>{% set config = craft.globals.getSetByHandle('menuCard') %}
  {% set file = config.menuCard.first() %}
  {% redirect file.getUrl() 302 %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-06T21:01:56.237Z
Answers.LastActivityDate: 2017-12-06T21:34:34.500Z
Answers.LastEditDate: 2017-12-06T21:34:34.500Z
Answers.UserDisplayName: Robin Schambach
Answers.Body: <p>You could setup a template at /menu that grabs the PDF you want and redirects with a 302 (not a 301 since it isn't permanent).</p>
  
  <p>The URL would change, but that might be ok if you only care about having an easy link to share.</p>
  
  <p>If redirecting is a deal breaker, you could make a small plugin that returns the file with proper headings, then call that from the /menu template.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-06T20:45:31.537Z
Answers.LastActivityDate: 2017-12-06T20:50:59.680Z
Answers.LastEditDate: 2017-12-06T20:50:59.680Z
Answers.UserDisplayName: Jim Butts
Body: <p>I have a field "menu card" in my globals' section "menuCard", so I could access my file in the template with <code>menuCard.menuCard.first().url</code>. That is fine, but when the editor changes the menu-card file and it has a different name, the url will be different:</p>
  
  <pre><code>first week:
  menu-card-17-10-01.pdf
  
  next week:
  menu-card-17-10-08.pdf
  </code></pre>
  
  <p>As it is a menu-card for a restaurant which gets changed every week it would be awesome to always have the current menu-card pdf available through the same url/slug like <code>myrestaurant.com/menu</code> and also reference the menu within my templates with <code>/menu</code>;</p>
  
  <p>Is there a way to do so in craftcms? For example an alias which points to my global field <code>menuCard.menuCard.first()</code> or something like that?</p>
  
  <p>Thx</p>
  
Tags: <routing><slug><files>
Title: How to point a static url to a file
ThreadText: # How to point a static url to a file
  
  (Asked by: username_0 on 2017-12-06)
  
  I have a field "menu card" in my globals' section "menuCard", so I could access my file in the template with `menuCard.menuCard.first().url`. That is fine, but when the editor changes the menu-card file and it has a different name, the url will be different:
  
  ```
  first week:
  menu-card-17-10-01.pdf
  
  next week:
  menu-card-17-10-08.pdf
  
  ```
  
  As it is a menu-card for a restaurant which gets changed every week it would be awesome to always have the current menu-card pdf available through the same url/slug like `myrestaurant.com/menu` and also reference the menu within my templates with `/menu`;
  
  Is there a way to do so in craftcms? For example an alias which points to my global field `menuCard.menuCard.first()` or something like that?
  
  Thx
  
  **Tags:** routing, slug, files
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-12-06. Score: 1 | Accepted answer
  
  You could create a simple plugin with [Pluginfactory](https://pluginfactory.io/) and copy the two function I'll give you to create the URL you like
  
  1. Go to the page, turn on the light switch for "Controller" but leave the name blank, download it, copy it to `craft/app/plugins/`
  2. Include this function to your plugins main file. If you did not disable preview/example code in the page you'll see there is already a `cocktails` route there
  
     ```
     public function registerSiteRoutes()
     {
         return [
             'menu' => ['action' => '>>insertYourPluginNameHere<</downloadMenuCard'],
         ];
     }
  
     ```
  3. in this case `www.exampleurl.de/menu` would download your file, you can change the key to whatever you want.
  4. Insert this function in your created Controller
  
     ```
     public function actionDownloadMenuCard()
     {
         $globalSetHandle = 'menuCard'; // change the handle of your global set here if needed
         $fieldHandle = 'menuCard'; // change the handle of your field here if needed
         $globals = craft()->globals->getSetByHandle($globalSetHandle);
         /** @var AssetFileModel $asset*/
         $asset = $globals->$fieldHandle->first(); 
         $source = $asset->getSource();
         $sourcePath = $source->settings['path'];
         $folderPath = $asset->getFolder()->path;
  
         $assetFilePath = $sourcePath.$folderPath.$asset->filename;
  
         craft()->request->sendFile($assetFilePath, IOHelper::getFileContents($assetFilePath));
     }
  
     ```
  
  You can include a redirect after that if you want in order to display the landing page or whatever you want. If you don't want to download the file at all and just display it you can do `craft()->request->redirect($asset->getUrl());` after you fetched the asset
  
  **Edit**
  
  Another approach without a plugin would be to add the route in your `craft/config/routes.php` file
  
  ```
  return array(
      'menu' => 'download.twig',
  );
  
  ```
  
  And do the redirect in the template
  
  ```
  {% set config = craft.globals.getSetByHandle('menuCard') %}
  {% set file = config.menuCard.first() %}
  {% redirect file.getUrl() 302 %}
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2017-12-06. Score: 1
  
  You could setup a template at /menu that grabs the PDF you want and redirects with a 302 (not a 301 since it isn't permanent).
  
  The URL would change, but that might be ok if you only care about having an easy link to share.
  
  If redirecting is a deal breaker, you could make a small plugin that returns the file with proper headings, then call that from the /menu template.
  
  
  ---
  
  