ComposerでCakephpにphpunitとdebugkitを入れてみる

ComposerでCakephpを入れてみたの続きです

phpunitとdebugkitを入れてみます

github上の
https://github.com/cakephp/cakephp/blob/master/composer.json
のcompoer.jsonをcakephpのディレクトリに保存します
(projectの下で、前回の続きだと「app」の中)

$ composer install
でphpunitとdebugkitが入ります

autoloaderの読み込み先を修正
$ vim Config/bootstrap.php

require ROOT . DS . APP_DIR . '/vendor/autoload.php';

phpunitが動くか確認します

http://(cakephpへのアクセス)/test.php
へブラウザでアクセス

cakephp-test

とでれば、うまくいってます

DebugKitの設定をします

$ vim Core/bootstrap.php

CakePlugin::load('DebugKit');       

を追記

$ vim Controller/AppController.php

class AppController extends Controller {
    public $components = array('DebugKit.Toolbar');                                                                                                                                                         
}

とcomponentを読み込みます
これですべての画面で有効になります

トップ画面にブラウザへアクセスして

cakephp-allgreen

オールグリーン!
で設定できました

ComposerでいれたCakephpを動かしてみる

ComposerでCakephpとPHPUnitを入れてみたの続きです

bake でプロジェクトを作ります
プロジェクト名はappにしました

/home/user/ws/vagrant/precise/cakephp/
がカレントディレクトリのときの実行結果です

$ Vendor/bin/cake bake project app

Welcome to CakePHP v2.6.1 Console
---------------------------------------------------------------
App : cakephp
Path: /home/user/ws/vagrant/precise/cakephp/
---------------------------------------------------------------
Skel Directory: /home/user/ws/vagrant/precise/cakephp/Vendor/cakephp/cakephp/lib/Cake/Console/Templates/skel
Will be copied to: /home/user/ws/vagrant/precise/cakephp/app
---------------------------------------------------------------
Look okay? (y/n/q) 
[y] > y
---------------------------------------------------------------
Created: app in /home/user/ws/vagrant/precise/cakephp/app
---------------------------------------------------------------
 * Random hash key created for 'Security.salt'
 * Random seed created for 'Security.cipherSeed'
 * Cache prefix set
 * app/Console/cake.php path set.
CakePHP is not on your `include_path`, CAKE_CORE_INCLUDE_PATH will be hard coded.
You can fix this by adding CakePHP to your `include_path`.
 * CAKE_CORE_INCLUDE_PATH set to /home/user/ws/vagrant/precise/cakephp/Vendor/cakephp/cakephp/lib in webroot/index.php
 * CAKE_CORE_INCLUDE_PATH set to /home/user/ws/vagrant/precise/cakephp/Vendor/cakephp/cakephp/lib in webroot/test.php
   * Remember to check these values after moving to production server
Project baked successfully!

Security.saltとSecurity.cipherSeedは勝手に生成してくれるようです

CAKE_CORE_INCLUDE_PATHを変えます

$ vim app/webroot/index.php

define('CAKE_CORE_INCLUDE_PATH',  ROOT . DS . 'Vendor' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib');

autoloaderを調整します

$ vim app/Config/bootstrap.php

// composerのautoloadを読み込み
require ROOT . DS . 'Vendor/autoload.php';

// CakePHPのオートローダーをいったん削除し、composerより先に評価されるように先頭に追加する
// https://github.com/composer/composer/commit/c80cb76b9b5082ecc3e5b53b1050f76bb27b127b を参照
spl_autoload_unregister(array('App', 'load'));
spl_autoload_register(array('App', 'load'), true, true);

ROOTが作成したappの一つ上、DSはDIRECTORY_SEPARATORで、ディレクトリの区切り文字です

データベースを用意します
mysqlを入れていたので、cakephpという名前でcreate databaseしておいて
設定ファイルのベースをコピー
$ cp app/Config/database.php.default
$ vim app/Config/database.php

データベース名、データベースユーザー、パスワードを変更します

で、画面にアクセスすると
cakephp-top

DebugKitを入れてない他はクリアしました

参考:
Cakephpでルーティングが動かない!? – bgbgbg

ComposerでCakephpを入れてみた

phpのパッケージマネージャーのComposerでCakephpとPHPUnitを入れてみました
Ubuntu 14.04で試しました

システム全体で使えるようにしました
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer

参考)https://getcomposer.org/doc/00-intro.md

これで
$ composer
だけで呼び出せます

インストールするものをcomposer.jsonに記載します
まず、cakephpを入れてみました

$ vim composer.json

{
    "name": "example-app",
    "repositories": [
        {
            "type": "pear",
            "url": "http://pear.cakephp.org"
        }
    ],
    "require": {
        "cakephp/cakephp": ">=2.4.9"
    },
    "config": {
        "vendor-dir": "Vendor/"
    }
}

参照) http://book.cakephp.org/2.0/ja/installation/advanced-installation.html

インストール
$ composer install
Loading composer repositories with package information
Initializing PEAR repository http://pear.cakephp.org
Installing dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - cakephp/cakephp 2.6.1 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.6.0 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.8 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.7 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.6 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.5 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.4 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.3 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.2 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.1 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.5.0 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.4.9 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.4.10 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - Installation request for cakephp/cakephp >=2.4.9 -> satisfiable by cakephp/cakephp[2.4.10, 2.4.9, 2.5.0, 2.5.1, 2.5.2, 2.5.3, 2.5.4, 2.5.5, 2.5.6, 2.5.7, 2.5.8, 2.6.0, 2.6.1].

エラー発生..mcryptがない..ということで

$ sudo apt-get install php5-mcrypt
再度
$ composer install

Loading composer repositories with package information
Initializing PEAR repository http://pear.cakephp.org
Installing dependencies (including require-dev)
  - Installing cakephp/cakephp (2.6.1)
    Downloading: 100%         

Writing lock file
Generating autoload files

cakephpが入りました
こんなディレクトリで入るみたいです

$ tree -d -L 5

.
└── Vendor
    ├── bin
    ├── cakephp
    │   └── cakephp
    │       ├── app
    │       │   ├── Config
    │       │   ├── Console
    │       │   ├── Controller
    │       │   ├── Lib
    │       │   ├── Locale
    │       │   ├── Model
    │       │   ├── Plugin
    │       │   ├── Test
    │       │   ├── Vendor
    │       │   ├── View
    │       │   ├── tmp
    │       │   └── webroot
    │       ├── lib
    │       │   └── Cake
    │       ├── plugins
    │       └── vendors
    └── composer

エラーが出たら
http://book.cakephp.org/2.0/ja/getting-started.html
http://book.cakephp.org/2.0/ja/installation/url-rewriting.html
を参考に

PHPExcelでセル内改行をする

PHPExcelを使ってセル内改行ではまったのでメモ

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$excel = $objReader->load($templatepath);
$excel->setActiveSheetIndex(0);
$sheet = $excel->getActiveSheet();
$sheet->setTitle($sheetName);
$value = '123'."\r".'';
$sheet->setCellValue('A8', $value);

と改行コードを \r を入れてもしても改行されない..
(Excelは \r らしい)

$obj->getActiveSheet()->getStyle(‘A8’)->getAlignment()->setWrapText(true);

が必要なようで、これをいれると改行されました

excel_eol