fc2ブログ

cakephp3でfixtureをdefaultのデータベースから作成する方法

cakephp3でfixtureをdefaultのデータベースから作成する方法のメモ。
cakephp2だと、対話形式でこれが出来たけど、cakephp3ではbakeする時にパラメータを付ける。
bin/cake bake fixture -r -n 100 -f -s モデル名
でOK。
-n オプションの100を10とかにすれば、10件分だけ作る。
オプションの一覧は下記。
--conditions      The SQL snippet to use when importing records.
(default: 1=1)
--connection, -c The datasource connection to get data from.
(default: default)
--count, -n When using generated data, the number of records to
include in the fixture(s). (default:
1)
--force, -f Force overwriting existing files without prompting.
--help, -h Display this help.
--plugin, -p Plugin to bake into.
--quiet, -q Enable quiet output.
--records, -r Generate a fixture with records from the non-test
database. Used with --count and --conditions to limit
which records are added to the fixture.
--schema, -s Create a fixture that imports schema, instead of
dumping a schema snapshot into the fixture.
--table The table name if it does not follow conventions.
--theme, -t The theme to use when baking code. (choices:
Bake|Migrations)
--verbose, -v Enable verbose output.
スポンサーサイト



cakephp3でcontorollerでelementを使う

cakephp3でcontorollerでelementを取得して、出力する方法をメモ。
$this->autoRender = false;
$this->response->type('json');
$view = new View();
$content = $view->element('my-element', $params);
$results =[
'status' => 'ok',
'html' => $content
];
$this->response->body(json_encode($results));
return $this->response;


参考サイト
CakePHPでHTMLの入ったJSONを返すAPIを作ってみた
CakePHP: How to use a view element inside of a controller
Can't access controller from element

CakePHP3 で bakeする時にフォルダに入れる

CakePHP3 で bakeする時にフォルダに入れる方法。
bin/cake bake controller Users --prefix Admin
のようにすると、
src/Admin/UsersController.php
が生成される。

mac El capitan でcomposer でcakephp3を入れる

mac El capitan でcomposer でcakephp3を入れるのに時間かかったのでメモ。
php composer.phar installで
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Your requirements could not be resolved to an installable set of packages.

Problem 1
- Installation request for cakephp/cakephp 3.0.12 -> satisfiable by cakephp/cakephp[3.0.12].
- cakephp/cakephp 3.0.12 requires ext-intl * -> the requested PHP extension intl is missing from your system.
Problem 2
- cakephp/cakephp 3.0.12 requires ext-intl * -> the requested PHP extension intl is missing from your system.
- cakephp/debug_kit 3.1.10 requires cakephp/cakephp 3.0.* -> satisfiable by cakephp/cakephp[3.0.12].
- Installation request for cakephp/debug_kit 3.1.10 -> satisfiable by cakephp/debug_kit[3.1.10].
で、うまく入らなかった。

手順としては
1. pear & pecl を使えるようにする
2. pecl で sudo pecl install intl
でオッケーなんだけど、sudo pecl install intl でこけまくった。

やったことと、表示されたエラーのメモと対処法
1.
sudo find / -name *intl.so
でintl.soがあるか確認。
※ macだと
/usr/local/php5-5.6.9-20150515-130841/lib/php/extensions/no-debug-non-zts-20131226/intl.so
にあったけど、これをphp.iniで指定してもダメだった。

2. sudo pecl install intlしたら
ERROR: unable to unpack /private/tmp/pear/download/intl-3.0.0.tgz
のエラー。
わからんけど、intl-3.0.0.tgzを削除して再度実行すれば出なくなった。

3. Xcode のコマンドラインツールをインストール
xcode-select --install
でインストール

4. php.ini にパス追加
include_path = ".:/usr/lib/php/pear"
extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20100525"


5. pear と pecl のアップデート
sudo pear channel-update pear.php.net
sudo pecl channel-update pecl.php.net
sudo pear upgrade-all


6.
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
の対処
wget http://jaist.dl.sourceforge.net/project/re2c/re2c/0.13.7.5/re2c-0.13.7.5.tar.gz
tar zxvf
cd re2c-0.13.7.5
./configure
make
sudo make install


7. ICU のインストール
configure: error: Unable to detect ICU prefix or no failed. Please verify ICU install prefix and make sure icu-config works.
ERROR: `/private/tmp/pear/temp/intl/configure --with-icu-dir=DEFAULT' failed
のエラーが出ていたのでインストール
curl -O http://download.icu-project.org/files/icu4c/52.1/icu4c-52_1-src.tgz
tar -zxvf icu4c-52_1-src.tgz
cd icu/source
./runConfigureICU MacOSX
make
sudo make install
ダウンロードする場所はその時の最新がよいかも。
http://site.icu-project.org/download

8. Autoconf のインストール
curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
tar -zxvf autoconf-latest.tar.gz
cd autoconf-2.69
./configure
make
sudo make install


9. ここまできてようやく
sudo pecl install intl

長かった・・・

10. sudo apachectl restart 最後にこれしてOK。

11.
php composer.phar install
PHP Warning:  PHP Startup: intl: Unable to initialize module
が出てたけど、これはphp.iniに書いたintl.soへのパスが違うため。


参考サイト
Installing the PHP intl extension on OS X Mavericks
PHP Warning: PHP Startup: ????????: Unable to initialize module
CONFIGURE: ERROR: UNABLE TO DETECT ICU PREFIX OR NO FAILED. PLEASE VERIFY ICU INSTALL PREFIX AND MAKE SURE ICU-CONFIG WORKS.
CentOSにPHPをソースからインストール
【Composer】MAMP環境にComposerでCakePHP3をインストールする

CAKEPHP2で特定のControllerとかactionだけBASIC認証をかける方法

CAKEPHP2で特定のControllerとかactionだけBASIC認証をかける方法のメモ。
Securityコンポーネント(CakePHP1系のみ)とか、Authコンポーネントでも出来るらしいけど、サクッと出来なかったのでこちらで対応。
これをAppControllerのbeforeFilterとか、かけたいactionとかで実行すればOK。

$loginId = 'hoge';
$loginPassword = 'fuga';

$this->autoRender = false;
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Private Page"');
header('HTTP/1.0 401 Unauthorized');
die("id / password Required");
} else {
if ($_SERVER['PHP_AUTH_USER'] != $loginId || $_SERVER['PHP_AUTH_PW'] != $loginPassword) {
header('WWW-Authenticate: Basic realm="Private Page"');
header('HTTP/1.0 401 Unauthorized');
die("Invalid id / password combination. Please try again");
}
}
$this->autoRender = true;


参考サイト
CakePHP:CakePHPでBasic認証を簡単に設定
[CakePHP][PHP]コンポーネントを利用せずにベーシック認証を実装する

プロフィール

U2K

Author:U2K
いろいろプログラムに関しての覚え書きをちょくちょくと書いていきます。

オススメ










オススメ

忍者AdMax

u2k on Twitter

最近の記事