2016年7月20日 星期三

Git 指令



  • git clone ${git.url} - 取出git專案
  • git commit ${file} -m "describe" - commit git檔案,只在本機端
  • git push -u origin master - 上傳commit的檔案到server端
  • git pull - 將server端的檔案update回本機端

2016年7月10日 星期日

錯誤:java.lang.NoClassDefFoundError: org/eclipse/core/resources/ResourcesPlugin

當啟動eclipse出現下方Error時
java.lang.NoClassDefFoundError: org/eclipse/core/resources/ResourcesPlugin


可嘗試到workspace下,刪掉*.snap的檔案

${workspace}/.metadata/.plugins/org.eclipse.core.resources/

2016年6月23日 星期四

Java Servlet HttpServletRequest getReader in Tomcat 筆記

HttpServletRequest.getReader() 

  • 在tomcat下,若post過來的資料有指定content-type=text/plain才抓得到資料。
    • 目前在tomcat6, tomcat8下測試都是此情形
  • 在resin下則是正常(resin 3.x)
  • 而在php下的話若content-type=text/plain,則要用file_get_contents('php://input')才取得到值。
  • 測試用php程式碼如下:

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "${url}" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "a=abc&body goes here" ); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain')); 

$result=curl_exec ($ch);
echo $result;
?>

2016年6月21日 星期二

Laravel HTTP Route (一)

基本說明

  • route資料定義在{laravel_dir}/app/Http/routes.php
  • 可指定get, post, put, delete等請求方式
  • 有哪些method可以使用,可以參考{laravel_dir}/vendor/laravel/src/Illuminate/Routing/Router.php

註冊路由



Route::get('/user/{userName}', function ($userName) {
.....
});
  • 當執行http://domain/user/test時,則$userName會被注入test這個值
  • 若使用post的方式存取http://domain/user/{userName}則會丟出MethodNotAllowedHttpException
  • 若userName為非必填則可將{userName}改為{userName?},並將callback function內的$userName給予一個預設值,ex: $userName = null



Route::post('/updateUser', function () {
    ...
});
  • 僅允許使用post的方式,若使用get的方式存取則會丟出MethodNotAllowedHttpException
  • 若使用任一html post到此網址,則會丟出TokenMismatchException ,防止CSRF(跨網站偽造請求)
    • Laravel會產生一個CSRF的token給予session做驗證使用,驗證是否為本身的app發出的Request
  • 在發出request的html內加入
    <input name="_token" type="hidden" value="<?php echo csrf_token(); ?>" />
    也可將該input的tag整個改為{{ csrf_field() }} (blade template engine 的語法),則會自動產生該句input
  • 若有url不要CSRF的保護可以去{laravel_dir}/app/Http/Middleware/VerifyCsrfToken.php內加入例外,該程式如下:

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
    '/updateUser'
    ];
}


2016年6月20日 星期一

Laravel Migration 使用


Migration的指令


在建立migration前,先確認config的user, password的設定是否符合資料庫系統,並確認database是否存在。
確認config可參考:http://kon-tw.blogspot.tw/2016/06/laravel-config-cache.html

建立migration (建立table)

php artisan make:migration create_order_table --create="order"

至於程式可以用哪些method 可以去看這支程式{laravel_dir}\vendor\laravel\framework\src\Illuminate\Database\Schema\Blueprint

建立完預設程式碼如下

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrderTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('order', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('order');
    }
}


對table插入一個新的欄位

php artisan make:migration add_member_id_to_order_table --table="order"


執行migration

php artisan migrate

  • 第一次執行migration時,laravel會在db自動建立一個table: migrations
  • 該table存放執行過哪些migration 的檔案
  • 下次執行就不會去執行已存在該table內的那些檔
  • 執行migrate時,是執行migration檔內up這個method。


Rollback migration

php artisan migrate:rollback

  • 當執行rollback時,會去執行各個migration檔內down這個method。並刪掉migrations內的資料。
  • 其rollback的順序是看migrations這個table內的batch,從數字最大的開始rollback。
  • 若batch內有1,2,第一次執行rollback,則會去執行batch=2的檔案,再執行rollback才會執行batch=1的檔案。
  • 會去執行migration檔內down這個 method

清除所有migration


php artisan migrate:reset


清除所有migration, 並重新執行migration


php artisan migrate:refresh



2016年6月19日 星期日

laravel artisan 執行流程


1. 使用{laravel dir}/bootstrap/app.php

2. 在app.php中會去建立一個Illuminate\Foundation\Application的物件

3. 該Application
        a. 繼承Container(Illuminate\Container\Container)
b. 實作 ApplicationContract(Illuminate\Contracts\Foundation\Application)
c. 實作HttpKernelInterface(Symfony\Component\HttpKernel\HttpKernelInterface)

4. app.php中,會再去建立三個singleton(在Container singleton的method)的物件, artisan用到的則     是console Kernel的部份
        a. Illuminate\Contracts\Http\Kernel
b. Illuminate\Contracts\Console\Kernel
c. Illuminate\Contracts\Debug\ExceptionHandler

5. {laravel dir}/artisan 去取得Console Kernel的singleton物件
        a. 執行app的handle method() (Illuminate\Foundation\Console\Kernel)
b. handle內會去new Illuminate\Console\Application(artisan主要程式
            繼承Illuminate\Console\Application)
c. 執行 Illuminate\Console\Application 內的method run();
d. run() 內會再執行Illuminate\Console\Application doRun 並回傳結果

2016年6月17日 星期五

laravel 重新建立config cache


Laravel的config檔的位置在{laravel}/config/*.php

在Laravel下若有使用config cache,其檔案位置會在{laravel}/bootstrap/cache/config.php下

當config有變動時,可以執行下列指令

php artisan config:cache

執行完後,若要看config是否有異動成自己想要的可以輸入下列指令

php artisan tinker

之後再輸入所要知道的config

ex:
config('database.connections.mysql')
config('app.env')

或者可以到{laravel}/bootstrap/cache/config.php去看資料是否有變動

PS: config中的env('DB_DATABASE', 'forge') 指的是當DB_DATABASE在.env裡沒被設定時才會給他預設值forge,即使DB_DATABASE=null, 所取得的也會是DB_DATABASE的空資料

若沒使用config cache, 則laravel會去抓config下相對應的{config}.php


2016年6月16日 星期四

Laravel 安裝

環境
  • PHP: Version 5.6.19
  • Apache: 2.4
  • OS: Win7

Larval 需求
  • PHP >= 5.5.9
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension
  • Tokenizer PHP Extension

若你的laravel專案的public不是在apache設定的根目錄,url會為
http://localhost/{安裝目錄}/public/
而public內的.htaccess並沒有真正發生作用,所以請在apahce的httpd.conf中加入vitrualhost,並重啟apache
<VirtualHost *>
    DocumentRoot "{laravel的public目錄}"
    ServerName kon.localhost
    <Directory "{laravel的public目錄}">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>