2018年5月6日 星期日

Larvel4.2 升級到5.0 5.2一些備註

larvel 升級的說明:


(一) 4.2升級到5.0

https://laravel.tw/docs/5.0/upgrade

大升級有幾個操作上文件沒有說到的部份要注意:

1.執行composer update時候出錯,

需要將vendor/compiled.php 移除,在重新執行composer update

2.注意控制器的命名空間問題

將所有的控制器移到 app/Http/Controllers 目錄下。既然在本指南中我們不打算整合到完整的命名空間,請將 app/Http/Controllers 添加到 composer.json 的 classmap,接下來,您可以從 app/Http/Controllers/Controller.php 基底抽象類別中移除命名空間,並確認整合過來的控制器是繼承這個基底類別。
在 app/Providers/RouteServiceProvider.php 檔案中,將 namespace 屬性設定為 null

3.注意CSRF問題

預設情況下,所有路由都會使用 CSRF 保護。若想關閉它們,或是只在特定路由開啟,請移除 App\Http\Kernel 中 middleware 陣列內的這一行:
'App\Http\Middleware\VerifyCsrfToken',

4.移除Form 跟 Html功能 要使用需要以下調整:

(1)在composer.json  裡面

 require 追加
        "laravelcollective/html": "~5.0"

使用composer update 更新

(2) config/app.php檔案裡面調整

 'providers' 追加     
        'Collective\Html\HtmlServiceProvider',

'aliases' 追加
        'Form' => 'Collective\Html\FormFacade',
        'Html' => 'Collective\Html\HtmlFacade',


(二) 4.2升級到5.2

https://laravel.tw/docs/5.0/upgrade

1.注意Input功能消失,要使用需要在 config/app.php檔案裡面調整:


 This commit removed Input facade definition from config/app.phphence you have to manually add that in to aliases array as below,
'Input' => Illuminate\Support\Facades\Input::class,
Or You can import Input facade directly as required,
use Illuminate\Support\Facades\Input;














2018年5月1日 星期二

Laravel controller,model更改目錄

laravel 4底下
預設controller目錄 app/controller
預設model目錄 app/model

如果要在兩個目錄底下在放資料夾,在資料夾底下放對應的controller,model 做法如下: 

app/controller/player 
app/model/player 

修改composer.json 檔案
在autoload追加classmap
 

"autoload": {
  "classmap": [
   "app/commands",
   "app/controllers",
   "app/controllers/player",
   "app/models",
   "app/models/player",
   "app/database/migrations",
   "app/database/seeds",
   "app/tests/TestCase.php"
  ]
 },



 調整完畢後,使用指令
composer dump-autoload

 原因:
 因為 database 文件夾使用 classmap 來做加載的。所以只有在打了composer dump-autoload之後  composer 才會更新 autoload_classmap 的內容。

2018年4月30日 星期一

laravel連不同資料庫 + mysql定時備份

定時備份請參考這個做法:
http://expect7.pixnet.net/blog/post/65167495

注意:可以手動匯出DB BackUP


使用Larvel可以連線不同DB 在app/config/database.php
設定不同連線

'mysql' => array(
'driver'    => 'mysql',
'host'      => 'localhost',
'database'  => 'white',
'username'  => 'xxxxxxxx',
'password'  => 'xxxxxxxx',
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => '',
),
'mysql_player' => array(
'driver'    => 'mysql',
'host'      => 'localhost',
'database'  => 'player',
'username'  => 'xxxxxxxx',
'password'  => 'xxxxxxxx',
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => '',
),


在程式當中使用連線可以做:

DB::connection('mysqlr')->table('white')->count(); 
DB::connection('mysql_player')->table('player')->count(); 











2018年4月7日 星期六

偵測使用裝置

偵測裝置有兩種做法:

1.jquery作法 : 使用navigator.userAgent

function get_device(){
    var ua = navigator.userAgent;
    var checker = {
      iphone: ua.match(/(iPhone|iPod|iPad)/),
      android: ua.match(/Android/)
    };
    if (checker.android){
        return "android";
    }
    else if (checker.iphone){
        return "iphone";
    }
    else {
        return "web";
    }   
}


2.PHP作法,使用mobile detect

參考說明:http://mobiledetect.net/

composer 下載指令:

composer require mobiledetect/mobiledetectlib


PHP範例:
$detect = new Mobile_Detect;

$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');

$scriptVersion = $detect->getScriptVersion();


echo "deviceType:".$deviceType."<br>";

echo "scriptVersion:".$scriptVersion."<br>";

echo "Iphone:".$detect->isIphone()."<br>";

echo "isAndroidOS".$detect->isAndroidOS()."<br>";

echo "isiOS".$detect->isiOS()."<br>";

// 注意

// $detect->isIphone()

// $detect->isAndroidOS()

// $detect->isiOS()

// 如果不符合則不會回傳任何資料,符合就會回傳1

2018年3月28日 星期三

PHP CURL作法說明 +SSL說明


因為現在各大Oauth授權都需要使用HTTPS方式進行。

PHP本身如果使用CURL呼叫HTTPS網址,可能產生網頁return沒有反應的情況。

需要加入以下兩條程式碼

    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);

範例程式如下


    $graph_url = "https://graph.facebook.com/me?access_token="
      . $params['access_token'];
    
    $curl = curl_init($graph_url);
    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $user_data = curl_exec($curl);



2018年3月24日 星期六

網路中文 網域+SSL憑證申請

網路中文網址:https://www.net-chinese.com.tw/nc/
SSL憑證說明:https://wiki.net-chinese.taipei/sites/wiki.net-chinese.taipei/files/public_files/application/SSL%E6%86%91%E8%AD%89%E7%B0%A1%E6%98%93%E6%95%99%E5%AD%B8(Linux%20Apache).pdf

1.首先向中華電信申請固定IP,將電腦連線使用固定IP連線

2.購買域名 (800元)

3.將域名使用DNS代管,並新增DNS記錄設定

設定二組A記錄指向網頁IP
(A記錄第一組主機名稱填www   指向IP
             第二組主機名稱空白       指向IP).




















4.DNS記錄需要一段時間生效24~48小時。

5.購買SSL憑證 (800元)。

6.在電腦安裝openSSL (  C:\openssl\bin ) 

7.在openSSL底下使用指令

openssl genrsa -des3 -out server.key 2048

設定PEM密碼,將產生的server key放在

8. 產生CSR文件

openssl req -new -key server.key -out certreq.txt

填入資料如圖
.

將產生出來的CSR文件,提供給網路中文,讓網路中文可以開始申請憑證。


9.網路中文會傳送需要驗證的信件,且會取得憑證。





























10.將取得的憑證安裝 +server key 放在在電腦的apache   EX:C:\Apache24\conf 。

















11.  設定 C:\Apache24\conf\extra  下的  httpd-ssl.conf  檔案

將前面的 # 拿掉以啟用,並設定剛剛的檔案位置

SSLEngine on SSLCertificateKeyFile:/etc/ssl/ssl.key/server.key (私鑰檔案路徑) SSLCertificateFile:/etc/ssl/ssl.crt/憑證.crt (憑證檔案路徑) SSLCertificateChainFile:/etc/ssl/ssl.crt/中繼憑證.ca-bundle(中繼憑證檔案路徑)


12. 設定 C:\Apache24\conf  下的  httpd.conf  檔案

將以下 #拿掉以啟用

 LoadModule ssl_module modules/mod_ ssl.so 的 #註解
 Include conf/extra/httpd-ssl.conf    的 # 註解
 
設定DocumentRoot "c:/website/www" 的網站目錄


13.重啟apache ,使用https連線確定能不能正常連入










2018年3月11日 星期日

laravel smtp mail發送錯誤


網路上不同的人做法:
http://shian420.pixnet.net/blog/post/344687356-%5Blaravel%5D-gmail-smtp-%E5%BE%9E%E8%A8%AD%E5%AE%9A%E5%88%B0%E7%99%BC%E4%BF%A1



自己測試用做法:
原本運作正常的PHP laravel mail突然開始跳出錯誤

laravel stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed



Laravel 4.2的解決辦法:

Edit \vendor\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php

line 259 ish. comment out the $options = array(); and add the below.


//$options = array();
$options['ssl'] = array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true);





如果使用gmail 需要在調整:

https://myaccount.google.com/u/1/lesssecureapps?pli=1&amp;pageId=none

 gmail的安全性:[允許安全性較低的應用程式] 設定處於啟用狀態 這樣才能登入 

PS用laravel設定寄信 app\config\mail.php設定裡面需要確定


'driver' ='smtp',
'host' ='smtp.gmail.com',
'username' ='xxxxxxxxxxxxxx',
'password' ='xxxxxxxxxxxx',

 自己寫的controller code



$from = ['email'=>'whitebaiw@gmail.com',
         'name'=>'whitebaiw',
         'subject'=>'主旨'
        ];
//填寫收信人信箱
$to = ['email'=>'zxcvbnm2749@gmail.com',
       'name'=>'zxcvbnm2749'];
//信件的內容(即表單填寫的資料)
$data = [
         'subject'=>'主旨',
         'msg'=>'訊息'
         ];
//寄出信件
Mail::send('web.message.mailShowView', $data, function($message) use ($from, $to) {
    $message->from($from['email'], $from['name']);
    $message->to($to['email'], $to['name'])->subject($from['subject']);
        });


----------------------------------------------------------------------------------------------------------

 如果是使用LARAVEL 5.2且使用ssl 方式寄信 需要調整.env 檔案:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=xxxxx@gmail.com
MAIL_PASSWORD=xxxx
MAIL_ENCRYPTION=ssl