コントローラーに定義したfunctionを呼び出す際、表記エラーになった。
function定義
function isAuthorizedUserProj($proj_id) {
......
}
ほかのfunctionから呼ぶ出す
if (isAuthorizedUserProj($id)) {
......
}
調べたところ、
In general you need to use $this-> when accessing class methods and variables.
ということで、if ($this->isAuthorizedUserProj($id))に変更すればOK
2017年3月27日月曜日
cakephp2のページ遷移
基本的に、actionごとにviewが用意されている。
場合によって、ほかのページに遷移する必要がある。そのとき、下記のように、「redirect」を使う。
$this->redirect(
array('controller' => 'Users', 'action' => 'login')
);
場合によって、ほかのページに遷移する必要がある。そのとき、下記のように、「redirect」を使う。
$this->redirect(
array('controller' => 'Users', 'action' => 'login')
);
htmlのimg tag内base64画像をサーバーに送信(cakephpの場合)
<img src="data:image/jpeg;base64,/9j.....=">
このsrcの画像をサーバーに送りたい。
JS側は以下のような感じ
function asyncSend() {
// Generate the image data
var base64image = $("#createdPic").attr('src');
// Sending the image data to Server
$.ajax({
method: "POST",
url: '/Projects/ajaxCall',
data: { "base64image" : base64image },
success: function (msg) {
console.log("OK");
},
error: function (response, desc, exception) {
console.log("NG");
}
});
}
サーバー側はcakephpを使用
public function ajaxCall() {
$this->autoRender = FALSE; //Viewある場合不要
if($this->request->is('ajax')) {
$img = $this->request->data['base64image'];
$img = str_replace('data:image/jpeg;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img); //base64_decode() でバイナリに戻す
$file = 'image.jpeg';
$success = file_put_contents($file, $data);
}
}
参考:http://stackoverflow.com/questions/1532993/i-have-a-base64-encoded-png-how-do-i-write-the-image-to-a-file-in-php
このsrcの画像をサーバーに送りたい。
JS側は以下のような感じ
function asyncSend() {
// Generate the image data
var base64image = $("#createdPic").attr('src');
// Sending the image data to Server
$.ajax({
method: "POST",
url: '/Projects/ajaxCall',
data: { "base64image" : base64image },
success: function (msg) {
console.log("OK");
},
error: function (response, desc, exception) {
console.log("NG");
}
});
}
サーバー側はcakephpを使用
public function ajaxCall() {
$this->autoRender = FALSE; //Viewある場合不要
if($this->request->is('ajax')) {
$img = $this->request->data['base64image'];
$img = str_replace('data:image/jpeg;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img); //base64_decode() でバイナリに戻す
$file = 'image.jpeg';
$success = file_put_contents($file, $data);
}
}
参考:http://stackoverflow.com/questions/1532993/i-have-a-base64-encoded-png-how-do-i-write-the-image-to-a-file-in-php
Chrome キャッシュ無効化
2回ほど、ブラウザのキャッシュデータにやられて、無駄な時間を使ってしまった。
Chrome キャッシュ無効化の方法をネットで見つかった。
検証→Settingのところで、「Disable cache (while DevTools is open)」のチェックを入れること。
参考:http://qiita.com/cubdesign/items/dca4d933539235bb5f11
Chrome キャッシュ無効化の方法をネットで見つかった。
検証→Settingのところで、「Disable cache (while DevTools is open)」のチェックを入れること。
参考:http://qiita.com/cubdesign/items/dca4d933539235bb5f11
2017年3月24日金曜日
cakephp2におけるユーザー認証(Auth)
usersテーブルが作成され、bakeしたことを前提とする。
1)usersテーブルに下記2つ項目を定義する必要がある。
「username」と「password」
2)UsersControllerに認証権限を設定
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add');
}
3)app/Controller/AppController.phpに「Auth」を追加(これらをUsersControllerに追加しても大丈夫)
class AppController extends Controller {
//...
public $components = array(
'Flash',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'posts',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
)
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
//...
}
4)UsersControllerにlogin/logoutを定義
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Invalid username or password, try again'));
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
5)User Modelでpasswordをhashする
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
class User extends AppModel {
// ...
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
return true;
}
1)usersテーブルに下記2つ項目を定義する必要がある。
「username」と「password」
2)UsersControllerに認証権限を設定
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add');
}
3)app/Controller/AppController.phpに「Auth」を追加(これらをUsersControllerに追加しても大丈夫)
class AppController extends Controller {
//...
public $components = array(
'Flash',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'posts',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
)
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
//...
}
4)UsersControllerにlogin/logoutを定義
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Invalid username or password, try again'));
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
5)User Modelでpasswordをhashする
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
class User extends AppModel {
// ...
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
return true;
}
6)login functionのviewを追加
<div class="users form">
<?php echo $this->Flash->render('auth'); ?>
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend>
<?php echo __('Please enter your username and password'); ?>
</legend>
<?php echo $this->Form->input('username');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
Authでログインしたユーザーのアクセスは以下2つ
// どこからでも利用できます。
AuthComponent::user('id')
// Controllerの中でのみ利用できます。
$this->Auth->user('id');
コントローラーで取得条件を指定することによって、ユーザーがどこまで権限があるか制御する
Authでログインしたユーザーのアクセスは以下2つ
// どこからでも利用できます。
AuthComponent::user('id')
// Controllerの中でのみ利用できます。
$this->Auth->user('id');
コントローラーで取得条件を指定することによって、ユーザーがどこまで権限があるか制御する
2017年3月23日木曜日
dpiに関して
canvasで出力された画像のdpiは96に固定されている。印刷するには、最低150dpiのものが必要とされている。どうすれば良いかいろいろ検討してみたが、結論として、パソコン画像に対して、dpiは意味を持っていない、ピクセルのみ意味がある。150dpi印刷するには、画像のdpiを上げることではなく、ピクセル数(サイズ)を上げれば良い。
以下のリンク、とても参考になった。
http://www.dpiphoto.eu/dpi.htm
以下のリンク、とても参考になった。
http://www.dpiphoto.eu/dpi.htm
2017年3月17日金曜日
登録:
投稿 (Atom)