Bootstrap3を使う
開発環境:CakePHP2.5.1
管理画面などを作るのに便利なCSSフレームワークのBootstrap。(記事公開時の最新バージョンは3.1.1)
CakePHPに簡単にBootstrapを導入する方法を見ていきましょう。
Bootstrapを設置する
サイトよりファイルをダウンロードします。
http://getbootstrap.com/
ダウンロードしたファイルを解凍し、css・fonts・jsをwebroot配下に設置します。
TwitterBootstrapプラグインを導入する
slywalkerさんが作成したTwitterBootstrapプラグインをダウンロードします。
https://github.com/slywalker/TwitterBootstrap
ダウロードしたファイルを解凍し、TwitterBootstrap とリネームして app/Plugin/TwitterBootstrap に設置します。
bootstrap.phpに読み込む設定を追記
設置したTwitterBootstrapを読み込むために、app/Config/bootstrap.php に下記のコードを追加します。
CakePlugin::load('TwitterBootstrap');
レイアウトをBootstrapに変更
slywalkerさんのプラグイン内にレイアウトファイルが入っていますが、Bootstrapのバージョン2のものなので、バージョン3にするなら app/View/Layouts/default.ctp を次のようなコードに変更します。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
<?php echo __('CakePHP: the rapid development php framework:'); ?>
<?php echo $title_for_layout; ?>
</title>
<!-- Bootstrap -->
<?php echo $this->Html->css('bootstrap.min'); ?>
<!-- Le styles -->
<style>
body {
padding-top: 50px;
}
.starter-template {
padding: 40px 15px;
text-align: center;
}
</style>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php echo $this->Session->flash(); ?>
<?php echo $this->fetch('content'); ?>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<?php echo $this->Html->script('bootstrap.min'); ?>
<?php echo $this->fetch('script'); ?>
</body>
</html>
カスタマイズは自由に!
ここまでで準備が完了したので、あとは公式に載っている例を見ながら部品を追加していきましょう。
http://getbootstrap.com/getting-started/#examples
