# เริ่มต้นใช้งาน Kotchasan Framework ## การสร้างโปรเจ็คใหม่ ### 1. สร้างโปรเจ็ค ```bash # สร้างโปรเจ็คใหม่ composer create-project kotchasan/framework my-app # เข้าไปในโฟลเดอร์โปรเจ็ค cd my-app ``` ### 2. ตั้งค่า Environment ```bash # คัดลอกไฟล์ environment cp .env.example .env # สร้าง application key php artisan key:generate ``` ### 3. ตั้งค่าฐานข้อมูล ```bash # รัน migration php artisan migrate # Seed ข้อมูลเริ่มต้น php artisan db:seed ``` ## โครงสร้างโปรเจ็ค ``` my-app/ ├── app/ │ ├── Controllers/ # Controllers │ ├── Models/ # Models │ ├── Middleware/ # Middleware │ └── Services/ # Business Logic ├── config/ # Configuration files ├── database/ │ ├── migrations/ # Database migrations │ └── seeders/ # Database seeders ├── public/ # Web root ├── resources/ │ ├── views/ # Blade templates │ └── assets/ # CSS, JS, Images ├── routes/ # Route definitions ├── storage/ # Logs, cache, sessions └── tests/ # Test files ``` ## การสร้าง Controller ### 1. สร้าง Controller ใหม่ ```bash php artisan make:controller HomeController ``` ### 2. เขียน Controller ```php 'หน้าแรก', 'message' => 'ยินดีต้อนรับสู่ Kotchasan Framework' ]; return $this->view('home', $data); } public function about() { return $this->view('about', [ 'title' => 'เกี่ยวกับเรา' ]); } } ``` ## การสร้าง Model ### 1. สร้าง Model ใหม่ ```bash php artisan make:model User ``` ### 2. เขียน Model ```php 'datetime', 'password' => 'hashed', ]; // Relationships public function posts() { return $this->hasMany(Post::class); } } ``` ## การสร้าง View ### 1. สร้างไฟล์ View ```html <?= $title ?>

คุณสมบัติ

  • ประสิทธิภาพสูง
  • ความปลอดภัย
  • ใช้งานง่าย

การใช้งาน

เริ่มต้นสร้างแอปพลิเคชันของคุณได้ทันที

``` ### 2. Layout Template ```html <?= $title ?? 'Kotchasan Framework' ?>
``` ## การตั้งค่า Routing ### 1. Web Routes ```php group(function () { Route::get('/posts', [PostController::class, 'index']); Route::get('/posts/{id}', [PostController::class, 'show']); Route::post('/posts', [PostController::class, 'store']); Route::put('/posts/{id}', [PostController::class, 'update']); Route::delete('/posts/{id}', [PostController::class, 'destroy']); }); ``` ## การทำงานกับฐานข้อมูล ### 1. Migration ```bash # สร้าง migration php artisan make:migration create_posts_table ``` ```php id(); $table->string('title'); $table->text('content'); $table->unsignedBigInteger('user_id'); $table->timestamps(); $table->foreign('user_id')->references('id')->on('users'); }); } public function down() { Schema::dropIfExists('posts'); } }; ``` ### 2. Seeder ```bash # สร้าง seeder php artisan make:seeder PostSeeder ``` ```php 'โพสต์แรก', 'content' => 'เนื้อหาของโพสต์แรก', 'user_id' => $user->id ]); } } ``` ## การใช้ Form และ Validation ### 1. สร้าง Form ```html
``` ### 2. Validation ใน Controller ```php validate([ 'title' => 'required|string|max:255', 'content' => 'required|string' ]); // Create post $post = Post::create([ 'title' => $request->title, 'content' => $request->content, 'user_id' => auth()->id() ]); return redirect('/posts/' . $post->id); } } ``` ## การใช้ Middleware ### 1. สร้าง Middleware ```bash php artisan make:middleware AuthMiddleware ``` ```php check()) { return redirect('/login'); } return $next($request); } } ``` ### 2. ลงทะเบียน Middleware ```php \App\Middleware\AuthMiddleware::class, 'guest' => \App\Middleware\GuestMiddleware::class, ]; ``` ### 3. ใช้ Middleware ใน Route ```php group(function () { Route::get('/dashboard', [DashboardController::class, 'index']); Route::get('/profile', [ProfileController::class, 'index']); }); ``` ## การใช้ Session และ Cookie ### 1. Session ```php $user->id]); session(['cart' => $cartItems]); // อ่านข้อมูลจาก session $userId = session('user_id'); $cart = session('cart', []); // ลบข้อมูลจาก session session()->forget('cart'); ``` ### 2. Cookie ```php put('user_posts', $posts, 3600); // ลบ cache ตาม tags Cache::tags(['posts'])->flush(); ``` ## การใช้ Queue ### 1. สร้าง Job ```bash php artisan make:job SendEmailJob ``` ```php email = $email; $this->subject = $subject; $this->message = $message; } public function handle() { // ส่งอีเมล mail($this->email, $this->subject, $this->message); } } ``` ### 2. Dispatch Job ```php delay(now()->addMinutes(5)); ``` ## การทดสอบ ### 1. Unit Test ```php 'John Doe', 'email' => 'john@example.com' ]); $this->assertEquals('John Doe', $user->name); $this->assertEquals('john@example.com', $user->email); } } ``` ### 2. Feature Test ```php create(); $response = $this->actingAs($user) ->post('/posts', [ 'title' => 'Test Post', 'content' => 'Test Content' ]); $response->assertStatus(302); $this->assertDatabaseHas('posts', [ 'title' => 'Test Post' ]); } } ``` ## การ Deploy ### 1. Production Setup ```bash # ตั้งค่า environment cp .env.example .env # แก้ไข .env สำหรับ production # Install dependencies composer install --no-dev --optimize-autoloader # Generate key php artisan key:generate # Run migrations php artisan migrate --force # Cache configuration php artisan config:cache php artisan route:cache php artisan view:cache ``` ### 2. Web Server Configuration ```nginx server { listen 80; server_name your-domain.com; root /path/to/kotchasan/public; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; fastcgi_index index.php; include fastcgi_params; } } ``` --- **หมายเหตุ**: นี่คือคู่มือเริ่มต้นใช้งาน สำหรับข้อมูลเพิ่มเติมโปรดดูที่ [API Reference](api-reference.md)