LoginSignup
1
1

More than 5 years have passed since last update.

Check! はじめての CakePHP2 ポイントまとめ 〜 Blog Tutorial を動かそう

Last updated at Posted at 2015-07-07

こんばんは、cloudpack@dz_ こと大平かづみです。

Prologue - はじめに

CakePHP2 に触れる機会があったのでメモをまとめます。ちなみに、最新バージョンの 3.x ではなく、2.x 系です。

今回は、手始めにチュートリアルのブログ作成を試しました。

ポイントまとめ

実は英語ページ読みながらやってたんですが、日本語ページがあったんですね(笑)

基本的な手順はドキュメントを参考いただくとして、記載されて内部分でつまづいたところの備忘録を書きます。

各種インストール

# apache インストール
yum install httpd

# mysql 関連のインストール
yum install mysql mysql-server

# php 関連のインストール
yum install php php-mysql php-pdo php-mbstring php-mcrypt php-xml

事前準備

  • mysql-server の初期設定
  • mysql ユーザ作成、データベース作成
    • テーブルデータを流すときに必要になりますので、一般的な手順で作成してください。
    • また、 database.phpユーザ名パスワードデータベース名 を記載する手順があります。
  • Apache 起動後、 phpinfo() などで、 pdo_mysqlmod_rewrite が組み込まれていることを確認してください。

CakePHP2の用意

(ドキュメントトップから読んでいたのに、このページなぜか読み飛ばしてました…)

トラブルシューティング

date.timezone でエラー

いざサンプルを動かしてみたら、以下のように date.timezone でエラーが発生した場合には、

Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for 'UTC/0.0/no DST' instead in /var/www/html/lib/Cake/Cache/CacheEngine.php on line 60

以下のように app/Config/core.php でタイムゾーンを指定できます。

app/Config/core.php
...

/**
 * Uncomment this line and correct your server timezone to fix
 * any date & time related errors.
 */
        //date_default_timezone_set('UTC');
        date_default_timezone_set('Asia/Tokyo');  // <---- 'Asia/Tokyo' を指定

...

もしくは /etc/php.ini を修正します。date.timezone = の部分がコメントアウトされているので、以下のように "Asia/Tokyo" を設定して下さい。

/etc/php.ini
[Date]
date.timezone = "Asia/Tokyo"

mode_rewrite のエラー

サンプルブログが動いたものの、CSSがあたってなくて、以下のエラーが表示されているときは、 URLリライティングがうまく行っていません。

URL rewriting is not properly configured on your server.    1) Help me configure it 2) I don't / can't use URL rewriting

そんなときは以下を参考に、 httpd.confAllowOverride の設定や .htaccess の記述を見直してください。

AllowOverrideの設定は複数箇所あるので、確認してみて下さい。

プラグインのインストールでエラー (phpunit, ext-mcrypt, ext-dom)

公式のプラグイン DebugKit などをインストールしようと、 php composer.phar install を行うと以下のエラーが返されました。この場合は php-mcryptphp-xml をインストールしてください。

Loading composer repositories with package information
Installing dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - The requested PHP extension ext-mcrypt * is missing from your system.
  Problem 2
    - phpunit/phpunit 3.7.9 requires ext-dom * -> the requested PHP extension dom is missing from your system.
...

DebugKitがインストールされない

DebugKitプラグインをインストールしたはずなのに、以下のようにインストールされていないことになっていました。

DebugKit is not installed. It will help you inspect and debug different aspects of your application.

どうやら、プラグインのロードが行われていません。こちらのCakePHP2系へのDebugKitを導入を参考に以下を対処しました。

app/Config/bootstrap.php
...

/**
 * Plugins need to be loaded manually, you can either load them one by one or all of them in a single call
 * Uncomment one of the lines below, as you need. Make sure you read the documentation on CakePlugin to use more
 * advanced ways of loading plugins
 *
 * CakePlugin::loadAll(); // Loads all plugins at once
 * CakePlugin::load('DebugKit'); //Loads a single plugin named DebugKit
 *
 */
CakePlugin::load('DebugKit');    // <---- ロード処理を明記

...
app/Controller/AppController.php
class AppController extends Controller {
  var $components = array('DebugKit.Toolbar');    // <---- コンポーネントを追加
}

DebugKitが見つからない

今度はプラグインのロードに失敗しました。

Error: The application is trying to load a file from the DebugKit plugin

Error: Make sure your plugin DebugKit is in the app/Plugin directory and was loaded

どうやら、CakePHPは <DocumentRoot>/app/Plugin を見に行くのに、composer でインストールしたプラグインは <DocumentRoot>/Plugin に格納されたので見つからなかったようです。私は以下のようにシンボリックリンクを貼りました。

# <DocumentRoot>/app/Plugin/DebugKit に <DocumentRoot>/Plugin/DebugKit へのシンボリックリンクを作成
ln -s /var/www/html/Plugin/DebugKit app/Plugin/DebugKit

Epilogue - おわりに

最近少しずつウェブサーバーを立てるのが早くなってきました(まだまだですが)。はじめてシリーズもだんだんと雑に…もとい手慣れてきました。そろそろ、はじめてシリーズ脱却か!?精進します!

近況

最近

Phalcon関連

fluentd関連

技術ブログ寄稿

cloudpack技術ブログでも、AWS Lambda や 運用ツール Serf に関して記事を書いています。ご興味あれば読んでいただけると嬉しいです!

1
1
0

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
1
1