Tuesday, September 1, 2015

วิธีการ Clone โปรเจ็คจาก BitBucket

เริ่มแรกเราจะต้องทำการอัพไฟล์ หรือ โฟลเดอร์ขึ้น bitbucket ก่อน โดยศึกษาจากลิ้งด้านล่างนี้
ไปยังวิธีการอัพโหลด  

                      
                      1. ไปยังหน้าหลักของ BitBucket แล้วกดไปยังหน้าเมนู Clone

                      2. คัดลอก link ที่ BitBucket ให้มา

                      3. เปิด Terminal แล้วไปยัง Directory ที่ต้องการ

                      4. นำลิ้งที่ได้ มาวางไว้ใน Terminal

                      5. ใส่รหัสผ่านของ BitBucket แล้วรอจนกว่าระบบจะ Clone เสร็จ

                      6. เมื่อเสร็จสิ้นเราก็จะทำการเปลี่ยนแปลงที่จัดเก็บไฟล์ / ที่อยู่ให้ตรงกับ Directory ที่                           เรานำมา Clone
                      
                      7. มีอยู่ 2 ไฟล์ 1. ไฟล์ laravel.conf ที่อยู่ใน /etc/apache2/site-available/laravel.conf
                                              2. ไฟล์ apache2.conf ที่อยู่ใน /etc/apache2/apache.conf

                      8. ในไฟล์ laravel.conf

                                              DocumentRoot /var/www/apartment/
                                       <Directory /var/www/apartment>
                                       Options Indexes FollowSymLinks MultiViews
                                       AllowOverride All
                                       Order allow,deny
                                       allow from all
                                        </Directory>
                       9. ในไฟล์ apache.conf
                                             <Directory /var/www/apartment>
                                                 Options Indexes FollowSymLinks
                                                 AllowOverride All
                                                 Require all granted
                                              </Directory>
                      
                        10. ทดสอบโดยการเข้าไปยัง Browser แล้วพิม localhost ถ้าขึ้นหน้าเว็บที่เรา                                           ต้องการแสดงว่าถูกต้องเรียบร้อย ขอบคุณครับ ^^  
 

Wednesday, August 19, 2015

การแก้ปัญหา mySQL ERROR 1045 (28000): Access denied for user 'root'@'localhost

การแก้ปัญหา  mySQL ERROR 1045 (28000): Access denied for user 'root'@'localhost [Phpmyadmin]

เปิดโปรแกรม Terminal พิมคำสั่ง

sudo service mysql stop 

sudo mysqld --skip-grant-tables 

mysql -u root

ถ้าสามารถใช้งานได้ จะสามารถเข้าไปที่ mysql> ได้เลย 




หลังจากนั้นก็สามารถใช้งาน

sudo php artisan migrate

ได้ตามปกติ

ถ้าไม่ได้ให้เพิ่มคำสั่ง

sudo chown -R www-data: app/storage

sudo chmod -R 775 app/storage


 

การแก้ไข Directory Apache [Ubuntu]

การแก้ไข Directory Apache [Ubuntu]

เมื่อเราทำการ นำเอาโปรเจคอื่นเข้ามา เราจะต้องแก้ Directory เพื่อให้ localhost ของเรามองเห็น

หลังจากที่เราแตกไฟล์ ได้โปรเจ็คใหม่ของเรามาแล้ว ให้ทำการ 
 
เปิดโปรแกรม Terminal  
 พิมคำสั่ง

  sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/laravel.conf


sudo nano /etc/apache2/sites-available/laravel.conf


จากนั้น ให้ทำการแก้ไขไฟล์

DocumentRoot /var/www/...ชื่อโปรเจ็ค.../
<Directory /var/www/...ชื่อโปรเจ็ค...>
 Options Indexes FollowSymLinks MultiViews
 AllowOverride All
 Order allow,deny
 allow from all
</Directory>

จากนั้น Restart Apache

sudo service apache2 restart

จากนั้นทำการเข้าไปเพื่อทำการ migrate ได้ที่

cd /var/www/...ชื่อโปรเจ็ค...  

พิมคำสั่งเพื่อสร้าง Database , และใส่ข้อมูล

sudo php artisan migrate 


php artisan db:seed




Monday, August 17, 2015

การใช้งาน Theme Bootstrap Backend

การใช้งาน Theme Bootstrap Backend[Laravel4][Laravel][Ubuntu]

การทดสอบนี้ เป็นการใช้ theme ของ Edmin ซึ่งสามารถทำการดาวน์โหลดได้ ที่นี่

เมื่อทำการดาวน์โหลดมาแล้วจะได้ไฟล์ edmin.zip

ก๊อบไฟล์ edmin.zip ที่ได้นำไปไว้ที่ /var/www/laravel/public/

เปปิดโปรแกรม Terminal

ใช้คำสั่งเพื่อไปที่เก็บไฟล์

cd /var/www/laravel/public  

unzip edmin.zip

mv bootstrap/* .

rm -rf bootstrap/ 

จากนั้นทำการสร้าง folder ใหม่ขึ้นมา เก็บไว้ใน /var/www/laravel/app/views/ 

 ชื่อ theme


ก๊อบไฟล์ ที่อยู่ใน /var/www/laravel/public/code/ ซึ่งประกอบด้วย .html ไปใส่ไว้ใน Theme  
(มีทั้งหมด 12 ไฟล์)
ทำการเปลี่ยนนามสกุลไฟล์ทั้งหมด จาก .html เป็น blade.php 

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

 Route::get('/activity', function()
{
    return View::make('Theme.activity');
});
Route::get('/charts', function()
{
    return View::make('Theme.charts');
});
Route::get('/form', function()
{
    return View::make('Theme.form');
});

Route::get('/index', function()
{
    return View::make('Theme.index');
});
Route::get('/message', function()
{
    return View::make('Theme.message');
});
Route::get('/other-login', function()
{
    return View::make('Theme.other-login');
});
Route::get('/other-user-listing', function()
{
    return View::make('Theme.other-user-listing');
});
Route::get('/other-user-profile', function()
{
    return View::make('Theme.other-user-profile');
});
Route::get('/table', function()
{
    return View::make('Theme.table');
});
Route::get('/task', function()
{
    return View::make('Theme.task');
});
Route::get('/ui-button-icon', function()
{
    return View::make('Theme.ui-button-icon');
});
Route::get('/ui-typography', function()
{
    return View::make('Theme.ui-typography');
});


จากนั้นทำการแก้ไขไฟล์ ที่อยู่ใน folder Theme ทั้งหมด

แก้ไข

ส่วนของ Head ให้เป็นดัง Code ข้างล่าง

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edmin</title>
    <link type="text/css" href="code/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link type="text/css" href="
code/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet">
    <link type="text/css" href="
code/css/theme.css" rel="stylesheet">
    <link type="text/css" href="
code/images/icons/css/font-awesome.css" rel="stylesheet">
    <link type="text/css" href='
code/http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600' rel='stylesheet'>
</head>



จากนั้น เติม


code/ ไว้ใน Url ของรูปทุกเช่น
จาก user.png เป็น code/user.png

แก้ไขคำสั่งที่มี .html ให้ลบ .html ออก

ทำให้ครบ 12 ไฟล์ 
สามารถเปิดการใช้งาน Url ได้ที่ : http://localhost/index 


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

การสร้างฟอร์ม Login / Register [Ubuntu][php][Laravel4][Sentry]

กระทู้ก่อนหน้า การสร้าง Database สำหรับ Sentry

ทำการแก้ไขไฟล์ 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('/');
    }
   
}



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

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

ทำการเพิ่ม Source 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> 


หลังจากนั้นทำการสร้าง file login
โดยสร้าง Folder ใหม่ ไว้ใน /var/www/laravel/app/views/
ชื่อ Folder ว่า home 
และทำการส้รางไฟล์ใหม่ขึ้นมาชื่อว่า  login.blade.php

@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>


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

@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


หลังจากนั้น ทำการแก้ไขไฟล์ routes.php ซึ่งอยู่ใน  /var/www/laravel/app/

<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/
Route::get('/', function()
{
    return View::make('hello');
});
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');
});



หลังจากนั้น ทำการแก้ไขไฟล์ fifters.php ซึ่งอยู่ใน  /var/www/laravel/app/

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



จากนั้นสร้าง 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



สุดท้ายทำการสร้างไฟล์ชื่อว่า AdminController.php  ไว้ใน /var/www/laravel/

<?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


ขอขอบคุณ Source Code จาก คุณ..fallendown


Friday, August 14, 2015

การทำ Migrate Database by terminal[Ubuntu]

การทำ Migrate Tatabase by terminal[Ubuntu]

ทำการติดตั้ง

php artisan migrate:install

ทำการไป Folder ที่ต้องการสร้างโดยใช้คำสั่ง

cd /var/www/laravel/

ใช้คำสั่ง ในการสร้าง

sudo php artisan migrate:make Create_table_use

จะได้

จากนั้นไปทำการเปิดไฟล์ที่ได้ ซึ่งอยู่ใน /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');
    }

}


แล้วไปที่ไฟล์  database.php ซึ่งอยู่ใน /var/www/laravel/app/config/database.php

ทำการแก้ไขให้ตรงกับ Database ของเรา

'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  =>  'member_1',
            'username'  => 'root',
            'password'  => '1234',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

จากนั้น ให้พิมพ์คำสั่ง

php artisan migrate



หลังจากนั้นเมื่อไปเปิดที่ localhost จะได้ (กรณีนี้มีการแก้ไขเลยมีเลข 1 เกินมา)




การติดตั้ง Sentry กับ laravel 4

การติดตั้ง Sentry กับ laravel 4

ให้ทำการเปิด เปิด Code php ของ laravel
ซึ่งอยู่ที่ laravel ชื่อว่า  composer.json




ให้ทำการแก้ไขและเพิ่มคำสั่งไว้ใน "require" ดังคำสั่งข้างล่างนี้


"require": {
  "laravel/framework": "4.2.*",
  "cartalyst/sentry": "2.1.*"
 },

ถ้าบางเครื่องไม่ได้ทำการ Install composer มาก่อน ให้ทำตามนี้

sudo php composer install

หรือถ้าลงไปแล้วให้ทำการอัพเดทโดยใช้คำสั่ง

sudo php composer update
      
ให้ทำการเปิดที่เก็บไฟล์ laravel เอาไว้ ในที่นี้เก็บไว้ตาม ที่อยู่ข้างล่าง

/var/www/laravel/app/config/app.php

ให้ทำการเกิดเปิดไฟล์ app.php
 

ไปที่ 'providers' => array(   แล้วทำการเพิ่มคำสั่งนี้ไว้ในบรรทัดสุดท้าย

'Cartalyst\Sentry\SentryServiceProvider',

เลื่อนลงมาที่ 'aliases' => array( แล้วทำการเพิ่มคำสั่งข้างล่างไว้ในบรรทัดสุด้ทาย

'Sentry' => 'Cartalyst\Sentry\Facades\Laravel\Sentry',


ทำการเปิดไฟล์ /var/www/laravel/app/config/database.php

 ทำการแก้ไข ให้ตรงกับ Database ของเรา(ควรสร้าง Database ใหม่ ศึกษาได้ที่นี่...)

'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'test_create01',
            'username'  => 'root',
            'password'  => '1234',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),


ทำการเปิด terminal และไปยังที่เก็บไฟล์ของ laravel โดยใช้คำสั่ง

cd /var/www/laravel/ 

ทำการ migrate ไฟล์ออกมา 
 
php artisan migrate --package=cartalyst/sentry



ทำการ config ไฟล์

php artisan config:publish cartalyst/sentry 



เมื่อเปิดใน localhost จะได้ผลลัพธ์ดังนี้

หมายเหตุ
    คือที่อยู่ / ไฟล์ / ชื่อไฟล์
    คือไฟล์ที่ถูกแก้ไข/เพิ่ม