ก่อนอื่นให้ทำการติดตั้ง XAMPP
และ Composer
เมื่อทำการติดตั้งเสร็จแล้วตรวจสอบผ่าน Command โดยคำสั่ง
# แสดง Version ของ PHP php -version PHP 7.3.12 (cli) (built: Nov 19 2019 13:58:02) ( ZTS MSVC15 (Visual C++ 2017) x64 ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.12, Copyright (c) 1998-2018 Zend Technologies # แสดง Version ของ Composer composer --version Composer version 1.9.1 2019-11-01 17:20:17
เอกสารประกอบ Laravel 5.6 | เอกสารประกอบ Laravel 5.8
สร้าง Laravel Project และทำการ Run Laravel Project
# composer create-project --prefer-dist laravel/laravel blog "5.8.*" composer create-project --prefer-dist laravel/laravel 01-basic "5.8.*" # เข้าไปใน Folder Project ที่สร้างเสร็จแล้วสั่งให้ Server ทำงาน php artisan serve
โครงสร้างใน Folder Project
- app : เก็บ Model และ Controller- database : เก็บ Migration และ Seeding สำหรับสร้าง Table
- public : เก็บ CSS, JS ไฟล์ต่างๆ ที่ให้เข้าถึงได้ และ .htaccess ด้วย
- resource : เก็บส่วนการแสดงผล View
- routes : เก็บไฟล์ที่ใช้กำหนด Url
- storage : เก็บข้อมูล Caches, Session, ไฟล์ Blade Engine ที่ Complied
- tests : เก็บ Automated Test
- *.env : เก็บ Config Laravel กับ ฐานข้อมูล
การกำหนด Routing
อ่านเพิ่มเติม https://laravel.com/docs/5.8/routing
// แสดง Text
Route::get('user', function () {
return 'User Information';
});
// ส่งไปยัง View
// resource/view/user/information.php or resource/view/user/information.blade.php
Route::get('user', function () {
return view('user.information');
// ส่งค่าไปให้ View ด้วย
// return view('user.information')->with('key','value');
// หน้าที่รับไปใช้เรียก $key
});
// ส่งไปยัง Controller
// UserController -> function showInformation
Route::get('user', 'UserController@showInformation');
// รับค่าจาก Url
// UserController -> function showInformation($name)
Route::get('user/{name}', 'UserController@showInformation');
การสร้าง View
อ่านเพิ่มเติม https://laravel.com/docs/5.8/viewsการสร้าง View ให้สร้างใน resources/views/*
ถ้าสร้าง Folder เวลาอ้างให้ใช้ . เช่น resources/views/user/information.blade.php เวลาเรียกใช้ view('user.information')
// การเรียกใช้ภายใน PHP
// Blade in HTML
{{$name}}
// Normal PHP
<?= $name ?>
การใช้งาน Blade
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>@yield('title')</title>
</head>
<body>
<header>
@yield('header')
</header>
@yield('content')
<footer>
@yield('footer')
</footer>
</body>
</html>
@extends('master')
@section('title', 'Home Page')
@section('content')
<h1>Home Page</h1>
{{$name}}
@endsection
การสร้าง Controller
อ่านเพิ่มเติม https://laravel.com/docs/5.8/controllers# สร้างใน Folder Project # php artisan make:controller ShowProfile --invokable php artisan make:controller UserController
เปิดไฟล์ Controller แล้วสร้าง Function ข้างใน
function index() {
return 'Index User Controller
';
}
function showInformation($name) {
return '' . $name . '
';
}
function showInformation($name) {
return view('user.information')->with('name', $name);
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น