Blogブログ

Category: cakephp

DockerでLAMP環境構築~cakephp3のインストールまで

これまでMAMPで開発していたプロジェクトをDocker化したので、その備忘録です。 作ったものは下に置いています。 https://github.com/shnr/docker_lamp_2 Dockerコンテナ作成 今回作成する環境 php 7.1 mysql 5.6 cakephp3.4 phpmyadmin ディレクトリ構成 どのようにでも出来そうですが、下の記事がわかりやすかったので参考にさせてもらいました。 https://qiita.com/ciloholic/items/c9bbb604d8338551b5dc ├── db │   ├── Dockerfile │   ├── my.cnf │   └── mysql_data ├── docker-compose.yml ├── html │   └── www └── web ├── Dockerfile ├── base.conf └── php.ini 各設定ファイルの作成 下準備 プロジェクトのルートに移動し、各ディレクトリを作成します。 $ cd ~/Dev/proj/docker $ mkdir db $ mkdir web $ mkdir html […]

cakephpでQRコード生成

cakephpでQRコード生成MEMO。 – module Image_QRCode を使います。 download : http://pear.php.net/package/Image_QRCode/download document : https://wiki.php.net/pear/packages/image_qrcode /lib/ ディレクトリ配下に設置します。 入っていない場合は、pearが必要です。 yum -y install php-pear – Controller 今回はGETで渡された数列のIDをQRコード画像にして返します。 Layoutはajax用に設定し、余計なLayoutの表示を防ぎます。 public function get_qr(){ $this->layout = ‘ajax’; $this->set(‘some_url’, ‘http://hogehoge.com’); return; } – view 表示用のviewでライブラリを読み込みます。 require_once APPLIBS . ‘Image_QRCode-0.1.3/Image_QRCode-0.1.3/Image/QRCode.php’; $QR = new Image_QRCode(); $option = array( ‘module_size’ => 4, ‘image_type’ => ‘png’, ‘output_type’ => ‘display’, […]

cakePHP : Auto Loginの実装

cakePHP 2.4 で、Auto Loginを実装した。 その手順をメモ。 まず、使用したコンポーネントはこちら。 http://milesj.me/code/cakephp/auto-login ただしこれは注意書きにあるように、2.Xでは非推奨になっているので、最新版を下からいただく。 http://milesj.me/code/cakephp/utility こちらをダウンロードして、使うのはContoroller/Component/AutoLoginComponent.php。 ※探してみたところ、こちらにアップデートのお知らせがあった。 http://milesj.me/blog/read/upgrading-ajaxhandler-1.6-3 実装の方法は、上のページと変わらない様だ。 1.AppControllerでComponentの追加。 (username => emailとしてるは、認証時のユーザー名をemailしているため。) public $components = array( ‘AutoLogin’, // 追加 ‘Auth’ => array( ‘authenticate’ => array( ‘Form’ => array( ‘fields’ => array(‘username’ => ’email’) ) ), ‘authorize’ => array( ‘Actions’ => array(‘actionPath’ => ‘controllers’) ) ), ‘Session’ ); 2.loginフォーム(僕の場合はuser/login.ctp)にチェックボックスの追加 echo $this->Form->input(‘auto_login’, […]

cakePHP: update aco table without command.

コマンドラインが使えない環境でacoテーブルを更新するには、以下のような関数を追加して実行する。 例えば、studentモデルを追加する。 まず、user/reitnitDBにアクセスすることでDBが更新されるよう、usersContorllerに下を追加する。 最初にStudentsのみ追加。 public function beforeFilter() { parent::beforeFilter(); $this->Auth->allow(‘reinitDB’); } public function reinitDB() { $this->Acl->Aco->create(array(‘parent_id’ => 1, ‘alias’ => ‘Students’, ‘model’ => ”)); $this->Acl->Aco->save(); echo “all done”; exit; } user/reitnitDBにアクセスする。 acoテーブルに一行追加されているはず。 今回追加した分のIDを93とする。 下層のページのために、下記を追加。 $aliasesの配列に含まれるページをacoテーブルに追加していくもの。 public function reinitDB() { $aliases = array( ‘admin_index’, ‘admin_add’, ‘admin_edit’, ‘admin_delete’, ‘admin_view’, ‘admin_course_edit’, ‘admin_couse_take’, ‘index’, ‘add’, ‘edit’, ‘view’, ); foreach […]

cakePHP: ajaxでのデータ取得。

ajaxを使います。 /user/index で、Userと紐付くStudentをajaxを使って取得する。 データの取得は、/user/ajax_get_students/ から行うと仮定。 ・/user/index.ctp 下のようなプルダウンでユーザを選択する。 user 1 user 2 ajaxリクエスト関数を用意。 戻ってきたdata展開の処理は省きます。 $(“#userselect”).change(){ var userid = $(this).val();     $.ajax({ type:”POST”,         url: “”, data: { ‘user_id’: userid, }, success:function(data){ console.log(data); }, error :function(XMLHttpRequest,textStatus){ console.log(textStatus); }     }); } コントローラ。 ・UsersController class ・UsersController extends AppController { public $components = array(‘RequestHandler’); public function ajax_get_students($id = null) { if($this->RequestHandler->isAjax()){ $user_id […]

Cakephp : HABTM アソシエーションでのupdateAll

状況としては、UserモデルとStudentモデルがある。Userは複数のStudentを持つ。 さらに、Studentは複数のCourseを持つ。Courseは重複するのでHABTM Associationとして登録。 Studentは各コース毎に受講しているか休学してるかstatusで管理する。status = 1 -> 受講中 、status = 0 -> 休学とする。 受講、休学はこんなボタンを表示させて、それぞれ studentsコントローラのadmin_couse_take()で処理を行う。 ちなみに、コントローラ内のファンクションにこうやって複数引数を渡せることも知った。 // absent echo $this->Form->postLink(__(‘Absent’), array( ‘controller’ => ‘students’, ‘action’ => ‘couse_take’, $student[‘Student’][‘id’], $course[‘CoursesStudent’][‘id’], false, ‘admin’ => true ), null, __(‘Are you sure you want to retake the class?’)); // take echo $this->Form->postLink(__(‘Take’), array( ‘controller’ => ‘students’, ‘action’ => ‘couse_take’, $student[‘Student’][‘id’], $course[‘CoursesStudent’][‘id’], […]

Cakephp localize

Cakephpのローカライズ。 コンソールでの作業になるので、先ずは\app\console\に行ってパスを通す。 cd …\app\cnosole SET PATH=%PATH%;%CD% アプリのルートパスへ行き、コマンドを実行。 cake i18n 対話形式でプログラムが実行される。 [E]xtract POT file from sources What would you like to do? 先ずはPOTファイルの生成。 What is the path you would like to extract? [Q]uit [D]one そして抽出元を選択。 Would you like to extract the messages from the CakePHP core? (y/n) cakePHPからの翻訳を書き込む場合はy What is the path you would like to […]

cakephp image uplpoad 2

別テーブルで画像を管理、例えばuserとimagesテーブルで分けたい場合。 まずはimagesテーブルを作る。 CREATE table images ( `id` int(10) unsigned NOT NULL auto_increment, `model` varchar(20) NOT NULL, `user_id` int(11) NOT NULL, `name` varchar(32) NOT NULL, `image` varchar(255) NOT NULL, `dir` varchar(255) DEFAULT NULL, `type` varchar(255) DEFAULT NULL, `size` int(11) DEFAULT 0, `active` tinyint(1) DEFAULT 1, PRIMARY KEY (`id`) ); Image でactsAsを宣言。belongsToでユーザとのリレーションを構築。 imagesテーブルのuser_idで紐付けたいので、foreingkeyはuser_idで。 画像格納パスは画像ID毎になる。(webroot/files/thumb/image_id/xxxx.jpgのようなかんじ) ‘pathMethod’をflatにすれば、全ての画像は’path’のディレクトリ配下に一括格納。 public $actsAs […]

cakePHP: image upload

Uploadプラグインの中でも評価の高かった、 https://github.com/josegonzalez/cakephp-upload これを実装してみたのでメモ。 オプションも多く、使いやすそう。 今回使ったのは、以下のオプション。 ‘path’ => ‘{ROOT}webroot{DS}files{DS}thumb{DS}’, ‘fields’ => array( ‘dir’ => ‘image_directry’, ), ‘thumbnailSizes’ => array( ‘xvga’ => ‘1024×768’, ‘vga’ => ‘640×480’, ‘cubic’ => ‘200×200’, ‘thumb’ => ’80×80′ ) ‘path’ 画像の格納パス。この場合はアップロードされた画像はwebroot/files/thumb/~ ‘fields’ 画像名が入るDBフィールド。 ‘thumbnailSizes’ thumbnailSizeを使っうとそれぞれのサイズが{size-name}_file_name で生成。 サイズ指定のオプションは以下。 100×80 – アスペクト比を保ち、トリミングを行う [100×80] – 指定したアスペクト比にフィットさせる 100w – アスペクト比を保ち、幅100px 80h – アスペクト比を保ち、縦80px その他にも、画像のサイズの最大・最小値の指定やパーミッション、上書きの可否等も指定できるようだ。 また細かいバリデーションをかけてエラーメッセージの指定も可能。 例: public […]

cakephp memo : basic sql

・普通のSQL。アソシエーションがあるテーブルの値も自動で取得出来る。 fields, conditionsでselect, where. public function index(){ $users = $this->User->find(‘all’, array( ‘fields’ => array(‘*’), ‘conditions’ => array(‘group_id =’ => ‘2’), )); $this->set(‘users’, $users); } ・inner joinを使う場合。 public function index(){ $users = $this->User->find(‘all’, array( ‘fields’ => array(‘*’), ‘conditions’ => array(‘group_id =’ => ‘2’),             “joins” => array(                              array(“type” => ‘INNER’,                                    “table” => ‘somethings’,                                    “alias” => ‘Something’,                                    “conditions” […]

12