LoginSignup
2
0

More than 5 years have passed since last update.

Craft CMS のルーティングをファイルで管理する

Posted at

Craft CMS で月別アーカイブのページを作成する | mersy note
https://note.mersy418.com/article/craft-cms-monthly-archive

こちらのブログ記事で紹介されているルートの設定について、少し補足。

Craft CMS では管理画面の「設定 > ルート」で管理する以外に、config/routes.php に定義する形でもルートを設定できる。

リンク先にあわせて archive/YYYY/MM でアクセスすると、テンプレート archive/_monthly でレンダリングするのであれば

config/routes.php
<?php
return [
    // 月別アーカイブ
    'archive/<year:\d{4}>/<month:\d{2}>' => ['template' => 'archive/_monthly'],
];

と定義すればよい。

ここで記述している <year:\d{4}> は4桁の数字を変数 year として受け取ることを意味するため、テンプレート内で

<h1>{{ year }}{{ month }}月の記事一覧</h1>

のように利用できる。

管理画面から登録する方法だと数が多い場合に管理が煩雑になり、開発環境や本番環境など異なる環境に反映するための時間が必要になるため、状況に応じて使い分けると良い。

参考

Craft 3 ドキュメント | ルーティング > URL ルールによる高度なルーティング
https://docs.craftcms.com/v3/ja/routing.html#url-ルールによる高度なルーティング

2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0