bigmac-jp blog

web開発関連のメモ

centos7.2 + Apache2.4 + php7 + cakephp3 + mysql5.6 で管理画面を作ります。 その1

centos7.2 + Apache2.4 + php7.0 + cakephp3 + mysql5.6 で管理画面を作ります。

リポジトリを最新にする

sudo yum update

php7.0インストール

#epelリポジトリ追加
sudo yum install epel-release
#remi追加
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
#php7とパッケージをインストール
sudo yum install --enablerepo=remi,remi-php70 php php-devel php-pear php-mbstring php-pdo php-gd php-zip php-xml php-fpm php-mcrypt php-mysqlnd php-pecl-apcu php-pecl-zendopcache php-intl
#phpのバージョン確認
php --version
 PHP 7.0.30 (cli) (built: Apr 24 2018 21:28:23) ( NTS )

#php.ini修正
sudo vi /etc/php.ini
------------------------------------------------------------
date.timezone =  'Asia/Tokyo' に変更
extension=intl.so を87行目に追加
------------------------------------------------------------

Apache2.4インストール

sudo yum install httpd
httpd -v
 Server version: Apache/2.4.6 (CentOS)

cakephp3インストール

sudo curl -s https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
mkdir /var/www/cake
chown xxx:xxx /var/www/cake
cd  /var/www/cake
composer create-project --prefer-dist cakephp/app ./
composer install

apache設定変更

sudo vi /etc/httpd/conf/httpd.conf 
------------------------------------------------------------
変更
DocumentRoot "/var/www/html" ->  DocumentRoot "/var/www/cake"
変更
<Directory "/var/www/html"> ->  <Directory "/var/www/cake">
変更
AllowOverride none  -> AllowOverride all
------------------------------------------------------------
#apache起動
service httpd start

mysqlインストール

yum -y install wget
wget http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
rpm -Uvh mysql-community-release-el6-5.noarch.rpm
yum -y install mysql-community-server
mysql --version
 mysql  Ver 14.14 Distrib 5.6.40, for Linux (x86_64)
sudo /etc/init.d/mysqld restart
mysql_secure_installation
vi /etc/my.cnf
下記追加
--------------------------------------------------------------
[client]
default-character-set=utf8

[mysqld]
skip-character-set-client-handshake
character-set-server = utf8
collation-server = utf8_general_ci
init-connect = SET NAMES utf8

[mysqldump]
default-character-set=utf8

[mysql]
default-character-set=utf8
--------------------------------------------------------------
#mysql再起動
/etc/init.d/mysqld restart

cakephpの設定

vi /var/www/html/config/app.php

220行目あたりが'Datasources' = DBの接続設定
(仮)
'username' => 'root',
'password' => 'XXX', 
'database' => 'my_app',

'timezone' => '+09:00',

vi /var/www/html/config/bootstrap.php
------------------------------------------------------------
date_default_timezone_set('Asia/Tokyo');に変更
------------------------------------------------------------

ブラウザアクセス
cakeの初期画面が表示される。
Databaseにmy_appデータベースが存在しないため、エラーとなっているが、今回はここまで。

次回は"my_app"データベースの作成とドキュメントルートの変更を行う。

UITableViewCell

完成図はこんな感じ

f:id:mtaryo:20170101154858p:plain





プロジェクト新規作成 → 「SingleView Application」を選択 → product Nameは「SampleCell」で作成。

f:id:mtaryo:20170101155555p:plain



初期から存在するView Controllerに「TableView」をドラッグ&ドロップする。
f:id:mtaryo:20170101160018p:plain
ドロップしたTableVIewをViewControllerの全体に広げる。
f:id:mtaryo:20170101220107p:plain
TableViewの上にTableViewCellをドラッグ&ドロップする。
f:id:mtaryo:20170101220415p:plain

「Table View Cell」を選択し、Attributei nspecterの中のidentifierの項目に「Cell」を入力する。
f:id:mtaryo:20170105220247p:plain

ViewController.swiftを選択する。
下記のようにソースを修正する。

import UIKit

// ①修正(UITableViewDataSource, UITableViewDelegate)
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //②追加
    //テーブルの行数を返却する
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    //③追加
    //テーブルの行ごとのセルを返却する
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = String(indexPath.row)
        return cell
    }
}

②と③のメソッドはUITableViewDataSourceの必須メソッドなので、記述なしだとエラーになる。

UITableViewDelegateとUITableViewDataSourceプロトコルの処理を行うクラスを、UIViewControllerの継承先のViewContollerに関連づける。
「ViewControllerScene」ツリーの「View Controller」→「View」→「Table View」を選択した状態でControlキーを押しながら、「View Controller」にドラッグ&ドロップする。ポップアップ画面が表示されるので、「dataSource」を選択する。 ドラッグ&ドロップを再度行い、「delegate」を選択する。これで関連付けが完了する。

Xcodeで保存&ビルドを実行。
f:id:mtaryo:20170105233927p:plain

ios10のローカル通知 〜通知削除

UNUserNotificationCenterを使って複数のローカル通知登録した場合に、
コメントアウト&ビルドした後もコメントアウト前に登録した通知情報が残る&通知される。

mport UIKit
import UserNotifications

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        

        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound]){
            (granted, error)in
        }

        //バックグラウンドで60秒ごとに通知登録
        let content = UNMutableNotificationContent()
        content.title = "ローカル通知確認60s"
        content.body = "ローカル通知確認60s!!!!!!"
        content.sound = UNNotificationSound.default()
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (60), repeats: true)
        let requestIdentifier = "60sTrigger"
        let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
        center.add(request)

    //下記をコメントアウトしても一度でも登録した場合にコメントアウト後にも通知されてしまう。
         //バックグラウンドで70秒ごとに通知登録
        //let content70 = UNMutableNotificationContent()
        //content70.title = "ローカル通知確認70"
        //content70.body = "ローカル通知確認70!!!!!!"
        //content70.sound = UNNotificationSound.default()
        //let trigger70 = UNTimeIntervalNotificationTrigger(timeInterval: (70), repeats: true)
        //let requestIdentifier70 = "70sTrigger"
        //let request70 = UNNotificationRequest(identifier: requestIdentifier70, content: content70, trigger: trigger70)
        //center.add(request70)

そんな時は通知情報を削除するremoveAllPendingNotificationRequestsを実行すると通知前の情報は全て削除される。

identifierを指定して特定の通知のみを削除するremoveDeliveredNotifications(withIdentifiers:)もある。

viエディタコマンド 行コピー&ペースト

最近Linuxを使うことがあるが、使うたびに調べてるので特に忘れやすいものをメモ


1行をコピー

 yy

数字の行数をコピー

数字yy

ペースト

p

テラタームとかでサーバにつないでファイルを修正する時に、
マウスでコピー&ペーストできるけど改行コードが文字列でペーストされるから、
viコマンドをちゃんと使うようにするべき。

ios10のローカル通知

ios10から追加されたUserNotificationsを使ってローカル通知を試しに実装してみた。
ちなみにUILocalNotificationはiso10から非推奨になった。

import UIKit
import UserNotifications

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //通知許可設定
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound]){
            (granted, error)in
        }
    
        //contentの設定
        let content = UNMutableNotificationContent()
        content.title = "ローカル通知タイトル"
        content.body = "ローカル通知ボディ"
        content.sound = UNNotificationSound.default()
        
        //通知トリガー
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (10), repeats: false)
        
        //identifier
        let requestIdentifier = "sampleRequest"
        
        let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
        
        center.add(request)
        
    }
}

通知トリガーでrepeats: true にした場合にtimeIntervalが60sより早い場合(10sとか50s)はエラーになってしまうのは不明。
特に、ドキュメントには記載されていない感じだった。

上のサンプルコードは、基本的にはUILocalNotificationでも実装できたことUserNotificationsで実装されているだけ。

アプリがバックグラウンドにいる時にローカル通知がされるから、フォアグラウンドの場合は通知されない。
フォアグラウンド状態でも通知したい場合は、別途実装が必要になる。次の機会にその辺を調査する。