2018年5月10日 星期四

laravel class not found 問題

在laravel5 不管新增加什麼控制器/model 有用到classmap
請先打composer dump-autoload

避免出現class not found

bitbucket.org git免費教學

說明文 : https://ithelp.ithome.com.tw/articles/10156944
說明push失敗:https://stackoverflow.com/questions/38255655/trying-to-pull-files-from-my-github-repository-refusing-to-merge-unrelated-his

在bitbucket 申請完免費帳號後, 如果當初申請帳號,是使用第三方登入googel等,需要到setting設定你的密碼  。

->setting ->connect accounts ->connect github


連結完成後,就可以使用github帳號登入。

在本地電腦。

1. git init

2. git remote add origin 網址

3. git pull origin master --allow-unrelated-histories

4. 編輯vim ,ESC跳出,在底下打w 存檔,最後用q離開編輯頁面回到指令\。

5. git commit -m "first"

6. git pull origin master

7. git push origin master  應該就可以成功。

備註會有錯誤產生原因可能是因為:
repo上面已經有一個 readme檔案,導致衝突
顯示以下訊息:! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/******/*****.git
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
但又遇到問題了,error message如下:
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/*****/*****.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
輸入git pull origin master就解決了。 而會發生以上問題的原因,是因為有local branch 和 origin branch的問題。

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);