私は、グローバルメニューの “menuCard”セクションに “menu card”というフィールドがあるので、
menuCard.menuCard.first()。url
でテンプレートのファイルにアクセスすることができました。それは問題ありませんが、エディタがメニューカードファイルを変更したときに、別の名前を持つと、URLは異なります:
first week:
menu-card-17-10-01.pdf
next week:
menu-card-17-10-08.pdf
それは毎週変更されるレストランのメニューカードなので、 myrestaurant.com/menu
のような同じURL /スラッグで常に現在のメニューカードpdfを利用できることは素晴らしいでしょう。
/menu
でテンプレート内のメニューを参照してください。
職人技でそうする方法はありますか?たとえば、グローバルフィールド
menuCard.menuCard.first()
などを指すエイリアスですか?
THX
Pluginfactory
を使って簡単なプラグインを作成し、好きなURLを作成するための2つの機能をコピーすることができます
- Go to the page, turn on the light switch for “Controller” but
leave the name blank, download it, copy it to
craft/app/plugins/
-
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 acocktails
route therepublic function registerSiteRoutes() { return [ 'menu' => ['action' => '>>insertYourPluginNameHere<
-
in this case
www.exampleurl.de/menu
would
download your file, you can change the key to whatever you
want. -
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
編集
プラグインを使用しない別のアプローチは、 craft/config/routes.php
ファイルにルートを追加することです
return array(
'menu' => 'download.twig',
);
そしてテンプレートのリダイレクトを行います
{% set config = craft.globals.getSetByHandle('menuCard') %}
{% set file = config.menuCard.first() %}
{% redirect file.getUrl() 302 %}