[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

CakePHPでView内の共通部分を使いまわす方法

こんにちは。
開発の長谷です。

CakePHPでの開発をすることが多いのですが、
Viewを記述する際、今までずっとhead内やheader、footerなどの
共通部分を1つ1つのファイルに書き込んでいました。

ただ、このように書いてしまうと修正が必要になった場合、
1つ1つファイルを修正しないといけなくなったりして、とても大変で面倒です。。

このことからどうにかして共通部分を一纏めにできないかと思い色々と調べた結果、
方法を見つけましたので紹介します。

方法

1. 共通となるレイアウトのViewファイルをapp/View/Elements/ 配下に作成する

まず、app/View/Elements/ 配下に共通となるレイアウトを作成します。
ファイル名は通常のViewと同様{任意のファイル名}.ctpです。

今回はhead.ctp, header.ctp, footer.ctp としましょう。
通常のViewに書いていたhead, header, footerの部分をそれぞれコピペすればOKです。

ちなみに、app/View/Elements/ 配下にディレクトリを作成して、その下にViewファイルを置くことも可能です。
例:app/View/Elements/Admin/header.ctp など

2. 通常のViewで先ほど作成したファイルを読み込ませる

読み込ませたい部分に

// 共通head
<?php echo $this->element('head'); ?>

// 共通header
<?php echo $this->element('header'); ?>

// 共通footer
<?php echo $this->element('head'); ?>

と記載すると、先ほど作成したファイルを自動で読み込んでくれます。

また、app/View/Elements/Admin/ のようにディレクトリを作成している場合は

// 共通head
<?php echo $this->element('Admin/head'); ?>

// 共通header
<?php echo $this->element('Admin/header'); ?>

// 共通footer
<?php echo $this->element('Admin/head'); ?>

と記述すればOKです。

問題点

共通部分といっても一部分だけ違っていたりすることが多いと思います。
よくあるのはhead内のtitleタグです。
titleタグの要素の内容はページによって内容が違うことが多いですよね。

こういった場合も対応可能です。

$this->element()で第二引数に連想配列で["変数名" => "値"]とすると、
パラメータを渡すことができます。

使用例:

// element側(head.ctp)

<head>
	.......
	<title><?php echo $title; ?></title>
</head>
// 通常View側(呼び出し側)

<?php echo $this->element('head', ["title" => "トップ"]); ?>

といった感じで呼び出すこともできるので大変便利ですね。

以上です。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
3
読み込み中...
3 票, 平均: 1.00 / 13
7,916
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Tatsuya Hase

Joined Beyond Co., Ltd. as a new graduate.

We develop web systems (development of services and systems that run on browsers, such as web services, digital content, and business management systems) and game APIs (development of programs that communicate with application games).

We also develop private/custom apps for Shopify.

Originally worked at the Osaka office, but transferred to the Yokohama office in 2019.
His hobbies are baseball/karaoke/anime