Monday, January 11, 2016

การสร้างฟอร์ม Login / Register (Laravel4)(Ubuntu4)

การสร้างฟอร์ม Login / Register 

****ก่อนการสร้างฟอร์มต้องทำการสร้าง database และมี table แล้ว หากยังไม่ได้ทำสามารถศึกษาได้จาก
การสร้างdatabase และ การสร้าง table

1. ทำการเปิดไฟล์  HomeController.php ที่อยู่ใน /var/www/laravel/app/controllers/ เพื่อแก้ไข
ดังนี้ 
<?php
class HomeController extends BaseController {
    /*
    |--------------------------------------------------------------------------
    | Default Home Controller
    |--------------------------------------------------------------------------
    |
    | You may wish to use controllers instead of, or in addition to, Closure
    | based routes. That's great! Here is an example controller method to
    | get you started. To route to this controller, just add the route:
    |
    |    Route::get('/', 'HomeController@showWelcome');
    |
    */
    public function showWelcome()
    {
        return View::make('hello');
    }
    public function getRegister()
    {
        return View::make('home.register');
    }
   
    public function getLogin()
    {
        return View::make('home.login');
    }
    public function postRegister()
    {
        try
        {
            $user = Sentry::createUser(array(
                'first_name' => Input::get('first_name'),
                'last_name' => Input::get('last_name'),
                'email' => Input::get('email'),
                'password' => Input::get('password'),
                'activated' => true,
                ));
        }
        catch (Cartalyst\Sentry\Users\UserExistsException $e)
        {
            echo 'User Already Exists';
        }
    }
    public function postLogin()
    {
        $credentials = array(
            'email' => Input::get('email'),
            'password' => Input::get('password'),
            );
        try{
                $user = Sentry::authenticate($credentials, false);
                if($user)
                    {
                        return Redirect::to('admin');
                    }
            }
        catch (\Exception $e)
            {
                return Redirect::to('login')->withErrors(array('login' => $e->getMessages()));
            }
    }
    public function logout()
    {
        Sentry::logout();
        return Redirect::to('/');
    }
   


2. ทำการสร้างหน้า Login / Register
       โดยทำการ New folder ที่ /var/www/laravel/app/views/
ชื่อ folder ว่า layouts

ทำการสร้างไฟล์ ขึ้นมาชื่อ
default.blade.php

ทำการเพิ่ม code ลงไป ดังนี้
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    {{ HTML::style('//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css') }}
</head>
<body>
    @yield('content')
    {{ HTML::script('//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js') }}
    {{ HTML::script('//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js') }}
</body>
</html> 
 
ดังภาพที่ 1.1 
ภาพที่ 1.1
3. สร้าง file login
      โดยสร้าง Folder ใหม่ ไว้ใน /var/www/laravel/app/views/
      ชื่อ Folder ว่า home และทำการส้รางไฟล์ใหม่ขึ้นมาชื่อว่า  login.blade.php
ทำการเพิ่ม code ลงไป ดังนี้
@extends('layouts.default')

@section('content')

<div class="col-md-4 col-md-offset-4">
      <div class="panel panel-info">
        <div class="panel-heading">Please Login</div>
        <div class="panel-body">
            {{ Form::open(array('url' => 'login')) }}
            @if($errors->has('login'))
                <div class="alert alert-danger">
                    <a href="#" class="close" data-dismiss="alert">&times;</a>
                    {{ $errors->first('login', ':message') }}
                </div>
            @endif
            <div class="form-group">
                {{ Form::label('email', 'Email Address') }}
                {{ Form::text('email', '', array('class' => 'form-control', 'placeholder' => 'Email Address')) }}
            </div>
            <div class="form-group">
                {{ Form::label('password', 'Password') }}
                {{ Form::password('password', array('class' => 'form-control', 'placeholder' => 'Password')) }}
            </div>
            <div class="form-group">
                {{ Form::submit('Login', array('class' => 'btn btn-success')) }}
                {{ HTML::link('/', 'Cancel', array('class' => 'btn btn-danger')) }}
            </div>
            {{ Form::close() }}
        </div>
      </div>
</div>
 
ดังภาพที่ 1.2 


ภาพที่ 1.2

4. ทำการเพิ่ม Register ไว้ในที่เดียวกันโดยใช้ชื่อ  register.blade.php โดยเพิ่ม code ลงไป ดังนี้

@extends('layouts.default')

@section('content')

<div class="col-md-4 col-md-offset-4">
      <div class="panel panel-info">
        <div class="panel-heading">Please Register</div>
        <div class="panel-body">
            {{ Form::open(array('url' => 'register')) }}
            @if($errors->any())
                <div class="alert alert-danger">
                    <a href="#" class="close" data-dismiss="alert">&times;</a>
                    {{ implode('', $errors->all('<li class="error">:message</li>')) }}
                </div>
            @endif
            <div class="form-group">
                {{ Form::label('fname', 'First Name') }}
                {{ Form::text('first_name', '', array('class' => 'form-control', 'placeholder' => 'First Name')) }}
            </div>
            <div class="form-group">
                {{ Form::label('last_name', 'Last Name') }}
                {{ Form::text('last_name', '', array('class' => 'form-control', 'placeholder' => 'Last Name')) }}
            </div>
            <div class="form-group">
                {{ Form::label('email', 'Email Address') }}
                {{ Form::text('email', '', array('class' => 'form-control', 'placeholder' => 'Email Address')) }}
            </div>
            <div class="form-group">
                {{ Form::label('password', 'Password') }}
                {{ Form::password('password', array('class' => 'form-control', 'placeholder' => 'Password')) }}
            </div>
            <div class="form-group">
                {{ Form::submit('Register', array('class' => 'btn btn-success')) }}
                {{ HTML::link('/', 'Cancel', array('class' => 'btn btn-danger')) }}
            </div>
            {{ Form::close() }}
        </div>
      </div>
</div>


@stop

จะได้ดังภาพที่ 1.3
ภาพที่ 1,3

5. หลังจากนั้น ทำการแก้ไขไฟล์ routes.php ซึ่งอยู่ใน  /var/www/laravel/app/
โดยเพิ่ม code ลงไปในบรรทัดสุดท้ายของหน้าดังนี้ 

Route::get('register', 'HomeController@getRegister');
Route::get('login', 'HomeController@getLogin');
Route::post('login', 'HomeController@postLogin');
Route::post('register', 'HomeController@postRegister');
Route::group(array('before' => 'auth'), function(){
    Route::get('admin', 'AdminController@index');
    Route::get('logout', 'HomeController@logout');
});
ดังภาพที่ 1.4 

ภาพที่ 1.4
6. จากนั้น ทำการแก้ไขไฟล์ fifters.php ซึ่งอยู่ใน  /var/www/laravel/app/
โดยนำ code ดังนี้ ไปวางทับ code เดิม

<?php
/*
|--------------------------------------------------------------------------
| Application & Route Filters
|--------------------------------------------------------------------------
|
| Below you will find the "before" and "after" events for the application
| which may be used to do any work before or after a request into your
| application. Here you may also register your custom route filters.
|
*/
App::before(function($request)
{
    //
});
App::after(function($request, $response)
{
    //
});
/*
|--------------------------------------------------------------------------
| Authentication Filters
|--------------------------------------------------------------------------
|
| The following filters are used to verify that the user of the current
| session is logged into this application. The "basic" filter easily
| integrates HTTP Basic authentication for quick, simple checking.
|
*/
// Route::filter('auth', function()
// {
//     if (Auth::guest()) return Redirect::guest('login');
// });
Route::filter('auth', function(){
    if(!Sentry::check()) return Redirect::guest('login');
});
Route::filter('auth.basic', function()
{
    return Auth::basic();
});
/*
|--------------------------------------------------------------------------
| Guest Filter
|--------------------------------------------------------------------------
|
| The "guest" filter is the counterpart of the authentication filters as
| it simply checks that the current user is not logged in. A redirect
| response will be issued if they are, which you may freely change.
|
*/
Route::filter('guest', function()
{
    if (Auth::check()) return Redirect::to('/');
});
/*
|--------------------------------------------------------------------------
| CSRF Protection Filter
|--------------------------------------------------------------------------
|
| The CSRF filter is responsible for protecting your application against
| cross-site request forgery attacks. If this special token in a user
| session does not match the one given in this request, we'll bail.
|
*/
Route::filter('csrf', function()
{
    if (Session::token() != Input::get('_token'))
    {
        throw new Illuminate\Session\TokenMismatchException;
    }
}); 
 

7. จากนั้นสร้าง Folder ใหม่ ไว้ใน /var/www/laravel/app/views/
ชื่อ Folder ว่า admin 
ทำการส้รางไฟล์ใหม่ โดยใช้ชื่อว่า index.blade.php

@extends('layouts.default')

@section('content')


<h2>Admin Section</h2>

    {{ HTML::link('logout', 'Logout', array('class' => 'btn btn-warning'))}}


@stop 

8. ทำการสร้างไฟล์ชื่อว่า AdminController.php  ไว้ใน /var/www/laravel/app/controller.php โดย     เพิ่ม code ดังนี้ 

<?php
class AdminController extends BaseController {
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        return View::make('admin.index');
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('admins.create');
    }
    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        //
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        return View::make('admins.show');
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        return View::make('admins.edit');
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        //
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }


สามารถใช้งานได้โดยเข้าไปที่

Url Login : localhost/login

Url Register : localhost/register

**หากไม่สามารถเข้าได้ ให้ตรวจสอบดูว่า ติด permission หรือไม่
คำสั่งปลด permission คือ sudo chmod -R 777 ชื่อโฟลเดอร์/
เช่น sudo chmod -R 777 laravel/

การทำ Migrate Table database

การทำ Migrate Table database บน Ubuntu4 by Terminal
****ก่อนการสร้างTable ต้องสร้าง database ก่อน หากยังไม่ได้สร้างสามารถศึกษาได้จาก http://cnexplus.blogspot.com/2016/01/migrate-database.html

1. ทำการไป Folder ที่ต้องการสร้างโดยใช้คำสั่ง cd /var/www/laravel/  หากเข้าไปแล้วให้พิมพ์ sudo php artisan migrate:make Create_table_use  ดังภาพที่ 1.1,1.2


ภาพที่ 1.1
      จากนั้นไปทำการเปิดไฟล์ที่ได้ ซึ่งอยู่ใน /var/www/laravel/app/database/migrations/...

ทำการแก้ไขจาก

<?php

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

class CreateTableUser extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }

}


เป็น

<?php

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

class CreateTableUser extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
        Schema::create('users', function($table)
        {
            $table->increments('id');
            $table->string('fisrtname')->unique();
            $table->string('lastname');
            $table->string('email');
        });

    }

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

}

ดังภาพที่ 1.2 
ภาพที่ 1.2
2. เมื่อทำการแก้ไขแล้วให้เปิดไฟล์ database.php ซึ่งอยู่                    ใน/var/www/laravel/app/config/database.php เพื่อทำการ configเมื่อเปิดแล้วให้ทำการแก้ไขให้ตรงกับ Database ของเรา
'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  =>  'testlaravel',
            'username'  => 'root',
            'password'  => '12345',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
ดังภาพที่ 1.3 

ภาพที่ 1.3
3. เมื่อเสร็จแล้วให้กลับไปที่ Terminal อยู่จะอยู่ที่ /var/www/laravel แล้วพิมพ์ php artisan migrate --package=cartalyst/sentry เพื่อทำการ migrate ไฟล์ออกมา จะได้้ผลลัพธ์ดังภาพ 1.4 , 1.5

ภาพที่ 1.4


ภาพที่ 1.5

4. เมื่อทำการ migrateแล้ว ให้พิมพ์ php artisan config:publish cartalyst/sentry   เพื่อทำการ config ไฟล์ ดังภาพที่ 1.6 , 1.7

ภาพที่ 1.6


ภาพที่ 1.7

 5. เมื่อทำการ migrate แล้วจะได้ table ใน database ตามภาพที่ 1.8


ภาพที่ 1.8

============================END=============================



การทำ migrate database

การทำ migrate database บน ubuntu 4

1. เปิดโปรแกรม Terminal 
            พิมพ์ mysql -u ...ชื่อ.... -h ....ชื่อ localhost.... -p ลงไปใน Terminal ตัวอย่างเช่น
                         (mysql -u root -h localhost -p) เมื่อกด Enter ลงไปแล้วโปรแกรมจะให้ใส่รหัส                 ผ่านของ database จะได้ตามภาพ 1.1 
ภาพ 1.1
2. เมื่อทำตามขั้นตอนที่ 1 แล้วให้พิมพ์ select database เพื่อดูว่าเรากำลังเลือกdatabase ตัวใดอยู่ หากยังไม่ได้เลือกหรือยังไม่มีฐานข้อมูลจะขึ้นดังภาพ 1.2
ภาพ 1.2
3. ทำการสร้าง database โดยพิมพ์คำสั่ง create database ชื่อdatabase; เช่น create database testlaravel; ดังภาพ1.3
ภาพที่ 1.3
4. เลือกใช้ database ที่เราได้ทำการสร้างจากข้อที่ 3 โดยใช้คำสั่ง use ชื่อdatabase; เช่น use testlaravel; ดังภาพที่ 1.4
ภาพที่ 1.4
5. พิมพ์ select database(); หากทำมาถึงขั้นตอนนี้แล้วถ้าทำถูกต้องจะขึ้นดังภาพที่ 1.5

ภาพที่ 1.5
6. พิมพ์คำสั่ง quit เพื่อออกจากการสร้าง database หากสร้าง database สำเร็จ database จะถูกสร้างใน localhost/phpmyadmin ในที่นี้ได้สร้างdatabase ชื่อว่า testlaravel จะแสดงดังภาพที่ 1.6
ภาพที่ 1.6

==============================The End****==============================



Sunday, January 10, 2016

ติดตั้ง Sentry กับ Laravel4

การติดตั้ง Sentry กับ Laravel 4 (Ubuntu4)

1. เปิด code   php ของ laravel โดยทำการหน้าไฟล์ที่ชื่อว่า composer.json ตามภาพที่ 1.1

ภาพที่ 1.1
2. Copy Code ดังนี้ "cartalyst/sentry": "2.1.*"  ไปวางในส่วนของ require ต่อจาก "laravel/framework": 4.2.*",  ดังภาพที่ 1.2  
ภาพที่ 1.2
3. ทำการเปิดไฟล์ในโฟลเดอร์ laravel เปิดไฟล์ที่ชื่อว่า app.php โดยไฟล์ app.php จะอยู่ตามที่อยู่ด้านล่างนี้ /var/www/laravel/app/config/app.php เมื่อพบไฟล์ app.php แล้วให้หาบรรทัดที่เขียนว่า 'providers' => array( แล้วทำการเพิ่ม'Cartalyst\Sentry\SentryServiceProvider', ไว้บรรทัดสุดท้ายตามภาพที่ 1.3 
ภาพที่ 1.3
4. หาคำสั่ง 'aliases' => array( ในหน้าเดิม แล้วทำการเพิ่มคำสั่ง                        'Sentry' =>'Cartalyst\Sentry\Facades\Laravel\Sentry', ไว้ในบรรทัดสุดท้าย ตามภาพที่ 1.4,1.5

ภาพที่ 1.4


ภาพที่ 1.5

============================END=============================


Thursday, January 7, 2016

Git คืออะไร ? + พร้อมสอนใช้งานกับ laravel



Git คือ Version Control ตัวหนึ่ง ซึ่งเป็นระบบที่มีหน้าที่ในการจัดเก็บการเปลี่ยนแปลงของไฟล์ในโปรเจ็คเรา มีการ backup code ให้เรา สามารถที่จะเรียกดูหรือย้อนกลับไปดูเวอร์ชั่นต่างๆของโปรเจ็คที่ใด เวลาใดก็ได้ หรือแม้แต่ดูว่าไฟล์นั้นๆใครเป็นคนเพิ่มหรือแก้ไข หรือว่าจะดูว่าไฟล์นั้นๆถูกเขียนโดยใครบ้างก็สามารถทำได้ ฉะนั้น Version Control ก็เหมาะอย่างยิ่งสำหรับนักพัฒนาไม่ว่าจะเป็นคนเดียวโดยเฉพาะอย่างยิ่งจะมี ประสิทธิภาพมากหากเป็นการพัฒนาเป็นทีม


Bitbucket  คืออะไร
  
Bitbucket เป็นเว็บเซิฟเวอร์ที่ให้บริการในการฝากไฟล์ Git 
ความจริงหากพูดถึง git คนก็คงจะคิดถึง github เป็นอย่างแรก แต่ที่เราเลือกใช้ Bitbucket ก็เพราะว่ามัน "สร้าง Private Repository ฟรีไม่จำกัดจำนวนและขนาด" ในขณะที่ Github เน้นฟรีไปที่ Public Repository ถ้าจะสร้าง Private Repo ต้องจ่ายตังค์
โดยข้อจำกัดของ bitbucket คือจำนวนคนที่ Collaborate ในโปรเจค ต้องไม่เกิน 5 คน ไม่งั้นต้องจ่ายเงินเพิ่ม

สมัค  BitBucket
 เปิดเบราเซอร์ พิม url  https://bitbucket.org/account/signup/




กดปุ่ม Get started

กรอกข้อมูลลงไป จากนั้นกด sign up และเข้าไปยืนยันใน email

การสร้าง tame
เพื่อสะดวกในการทำงาน ด้วยกันหลายๆคน เราจำเป็นต้องสร้างทีมที่มาร่วมพัฒนา งานร่วมกัน
โดยใช้ repository เดียวกัน repository เป็นเหมือนตะกล้าเก็บโปรเจคและโคดต่างๆ ของเรา ไว้บนเซิฟเวอร์
ไปที่เมนู tames แล้วกด Create team


ใส่ tame name และใส่ id tame
และเพิ่มคนเข้ามาในทีมโดยใส่ email ของเขาลงในช่อง Add team members กดปุ่ม add และกดปุ่ม Create



 

สร้าง repositories

 

ไปที่เมนู tames และเลือกทีมที่เราสร้างมา

ไปที่เมนู repositories และกด Create  repository เพื่อสร้าง repository

 

เลือก owner ตั้งสื่อ Repository และเลือก Repository type เป็น git

 และเลือก Language เป็น  php จากนั้นกด Create  repository


 พอเสร็จขั้นตอนนี้ เราก็จะได้ พื้นที่ไว้เก็บ โปรเจคของเราแล้ว

 

เริ่มต้นติดตั้ง  Git 

พิมคำสั่งนี้ลงบน terminal

sudo apt-get install git
 
เมื่อดาวน์โหลดและติดตั้ง Git เรียบร้อยแล้ว สิ่งที่ต้องทำต่อมาก็คือ Setup ชื่อและอีเมล์สำหรับใช้งาน Git ครับ ตั้งค่าผ่าน Terminal 

git config --global user.name "YOURNAME"
git config --global user.email "your@email.com"
 

การอัพ laravel ลงบน bitbucket


เปิดเบราเซอร์ ไปที่ bitbucket.org และเลือก repository ที่สร้างไว้ ตรงเมนูด้านช้ายจะมีปุ่ม
Clone อยู่ พอกดเข้าไปจะมี คำสั่ง clone repository ลงบนเครื่องเรา ให้ก๊อบคำสั่งนั้นมา




จากนั้นเปิด terminal จะไปยังตำแหน่งที่เราอยากจะ clone ลงบนเครื่องเรา (เช่น cd /var/www)

แล้ว นำคำสั่งที่เรา คัดลอกมาวางลงไป (ctrl+shift+v) เราจะได้โฟเดอร์ใหม่ที่มีชื่อ ตาม
repository ที่เราสร้างไว้


 จากนั้น ย้ายตำแหน่ง terminal เข้าไปในโปรเจคที่เรา clone มา(เช่น cd /var/www/laravel4)
 จากนั้น พิมคำสั่ง 
                  sudo git in it

เสร็จแล้ว เข้าไปในโฟเดอร์ laravel และก๊อบ ไฟล์ทั้งหมดข้างในไปวางใน โฟเดอร์ที่ clone มา


ไปที่ terminal และพิมคำสั่ง sudo git commit -a กด ctrl + x และกด y เพื่อ save commit

 ทำการ Remode url

git remote add origin ….............Url ที่เก็บ

โดย url ของเรานั้นก็เอามาจาก ที่เดียวกับคำสั่ง clone 


และ ส่งไฟล์ไปให้เซิฟเวอร์

git push -u origin master 

 

ตรวจสอบ การอัพโหลด

ไปที่ bitbucket เลือกเมนู source ทางช้ายมือ จะแสดงไฟล์ทั้งหมดที่เราอัพโหลด

 

  อ้างอิงจาก 

http://devahoy.com/posts/introduction-to-git-and-github/

https://nuuneoi.com/blog/blog.php?read_id=703