Professional Documents
Culture Documents
Basic Laravel 8 and React JS With Docker Online
Basic Laravel 8 and React JS With Docker Online
Basic Laravel 8 and React JS With Docker Online
วิทยากร Frontend
Angular, React, NextJS, VueJS,
Bootstrap,TailwindCSS
Backend
PHP, Python, Java, NodeJS, ASP.net
อ.สามิตร โกยม
ปริญญาโทคณะเทคโนโลยีสารสนเทศ
Database
มหาวิทยาลัยเทคโนโลยีพระจอมเกล้าพระนครเหนือ MySQL, PostgreSQL, MS SQL Server,
MongoDB
Mobile
Java, Kotlin, Objective C, Swift, Cordova,
ionic, Flutter
DevOps
Git, Github, Gitlab, Docker,
Kubernetes
ดาวน์โหลดเอกสารประกอบการอบรม
bit.ly/3oZq9RI
www.itgenius.co.th | 02-570-8449 IT Genius Engineering | สถาบันไอทีจีเนียส
Course Outline
Basic Laravel 8 and React JS with Docker
https://docs.docker.com/desktop/windows/install/
https://github.com/docker-archive/toolbox/releases
https://www.youtube.com/watch?v=Uo5rkcZFHcA
https://www.youtube.com/watch?v=Lbr3FnsfAAU
1. Beautify by HookyQR
2. Color Picker by anseki
3. AutoFileName by JerryHong
4. Material Icon Theme by Philipp Kief
5. Docker by Microsoft
6. Laravel Snippets by Winnie Lin
7. Laravel Blade Snippets by Winnie Lin
8. Laravel Artisan by Ryan Naddy
9. ES7 React/Redux/GraphQL/React-Native snippets by dsznajder
10.html to JSX by Riaz Laskar
11.Bracket Pair Colorizer by CoenraadS
12.One Dark Pro by binaryify
การเปลี่ยน Theme และ Config ค่าเริ่มต้น
Docker
docker --version
VS Code
code -v
ติดตั้ง Node.js
Download Node.js V.14.15.x
https://nodejs.org/download/release/v14.15.0/
หมายเหตุ
node -v npx -v
npm -v
Windows Server
Ubuntu
CentOS
Debian
Hyper visor (vmware)
Hardware
คืออะไร
เมื่อต้องการนาไปใช้งานที่อื่นหรือบน
Production จริง ก็นาไปทั้งก้อน
Container นี้เลย ทาให้สภาพแวดล้อม
เหมือนกันทุกประการ
Container
Micro Computer
• OS
• CPU
Windows Server
Windows
• Mem
CentOS
Debian
Fedora
Ubuntu
CentOS
Debian
• Network
Docker Engine
Hyper visor (vmware) Ubuntu
Hardware Hardware
LXC (Linux)
containerd
$ docker ps $ docker ps -a
Start/Stop Container
Has
No
Image
$ docker pull <image> 2
Yes
$ docker create <container> 3
$ docker create <container> 2
$ docker start <container> 4
$ docker start <container> 3
Docker Image
Vote App
Ecommerce App
Container
Docker Image
Windows/macOS/Linux
Docker Machine / Host Machine
Container
80 80 80
Windows/macOS/Linux
Docker Machine / Host Machine
Container
8800 80 80
$ docker info
$ docker system df
CONTAINER
CONTAINER
CONTAINER
CONTAINER
CONTAINER
CONTAINER
DOCKER HOST
CONTAINERS
CONTAINER
CONTAINER
CONTAINER
# ติดตั้ง extension mysqli สำหรั บไว้ เชื่ อมต่ อกับฐำนข้ อมูล mysql/mariadb
RUN docker-php-ext-install mysqli
root /var/www/html;
index index.php;
location / {
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
}
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $uri =404;
#}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php:9000;
try_files $uri =404;
}
location ~ /\.(ht|svn|git) {
deny all;
}
}
services:
db:
image: mariadb:latest
container_name: lemp_mariadb
volumes:
- ./mariadb/initdb/:/docker-entrypoint-initdb.d
- ./mariadb/data/:/var/lib/mysql/
environment:
- MYSQL_ROOT_PASSWORD=1234
- MYSQL_DATABASE=sample_db
- MYSQL_USER=admin
- MYSQL_PASSWORD=1234
restart: always
networks:
- web_network
php:
depends_on:
- db
build: php/
container_name: lemp_php
volumes:
- ./public_html/:/var/www/html
expose:
- "9000"
restart: always
networks:
- web_network
nginx:
image: nginx:stable-alpine
container_name: lemp_nginx
volumes:
- ./public_html/:/var/www/html
- ./nginx/conf/nginx.conf:/etc/nginx/conf/nginx.conf:ro
- ./nginx/conf.d:/etc/nginx/conf.d:ro
ports:
- "88:80"
restart: always
networks:
- web_network
networks:
web_network:
name: lemp
driver: bridge
Create index.php
<?php
$servername = "db";
$username = "admin";
$password = "1234";
$ docker-compose config -q
การรัน docker-compose.yml เพื่อสร้าง images และ containers
$ docker-compose up -d
ทดสอบเรียกใช้งาน
ทดสอบเรียกใช้งาน
การบังคับให้ docker-compose re-build ใหม่
$ docker-compose up -d --build
การเรียกดู log
$ docker-compose logs <service_name>
CONTAINERS
CONTAINER CONTAINER
~7.0.1 ~7.0.4
^7.0.1 ~7.1.0
พื้นฐานการทางานกับ Routes ใน Laravel 8
Routes
Route::get('about', function(){
return 'My about page';
});
Routing with Parameters
Route::get('user/{id}', function($id){
return 'User: '.$id;
});
Route::get('posts/{post}/comments/{comment}', function(
$postID,$commentID){
return 'Post: '.$postID.'<br>Comment: '.$commentID;
});
Route::get('token', function(){
return csrf_token();
});
Route::post('product', function () {
return 'This is my product';
});
Route::put('product', function () {
return 'Update product';
});
Route::delete('product', function () {
return 'Delete product';
});
พื้นฐานการทางานกับ Controller และ View ใน Laravel 8
Route::get('/', 'App\Http\Controllers\HomeController@home');
Method 2:
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'home']);
Route
Route::get('user/{name?}', [HomeController::class, 'user']);
Controller
public function user($name=null){
return 'สวัสดี '.$name;
}
การทางานกับฐานข้อมูล
▪ พื้นฐานการทางานกับฐานข้อมูล (database)
▪ การทางานกับ Query Builder ใน Laravel 8
▪ การทางานกับ Database Eloquent / ORM
Route::resource('photos', PhotoController::class);
หรือเขียนแบบย่อ
docker-compose exec app php artisan make:model Product -mc --api
กาหนดโครงสร้าง migrations
create_users_table.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('fullname');
$table->string('username');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->tinyInteger('role')->default(1);
$table->rememberToken();
$table->timestamps();
});
}
กาหนดโครงสร้าง migrations
create_products_table.php
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->string('description')->nullable();
$table->decimal('price', 9, 2);
$table->timestamps();
});
}
migrate
docker-compose exec app php artisan migrate
www.itgenius.co.th | 02-570-8449 IT Genius Engineering | สถาบันไอทีจีเนียส
Enable Sanctum's middleware to your api middleware group
'api' => [
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
use Laravel\Sanctum\HasApiTokens;
$user = User::create([
'fullname' => $fields['fullname'],
'username' => $fields['username'],
'email' => $fields['email'],
'password' => bcrypt($fields['password']),
'tel' => $fields['tel'],
'role' => $fields['role']
]);
$token = $user->createToken($request->userAgent(),[$fields['role']])->plainTextToken;
$response = [
'user' => $user,
'token' => $token
];
// Check email
$user = User::where('email', $fields['email'])->first();
// Check password
if(!$user || !Hash::check($fields['password'], $user->password)){
return response([
'message' => 'Invalid login!'
], 401);
}else{
// ลบ token เก่ ำออกแล้ วค่ อยสร้ ำงใหม่
$user->tokens()->delete();
$token = $user->createToken($request->userAgent(),["$user->role"])->plainTextToken;
$response = [
'user' => $user,
'token' => $token
];
auth()->user()->tokens()->delete();
return [
'message' => 'Logged out'
];
}
CONTAINERS
CONTAINER CONTAINER
Module 4:
พื้นฐานการทางานกับ React.js
• ทาไมต้องใช้ JSX
• ข้อกาหนดพืน ้ ฐานของ JSX
• การแสดงค่าของตัวแปรใน JSX
• การคานวณค่าในและแสดงผล (Expression) ใน JSX
• การแสดงหรือซ่อนเนื้อหาใน JSX
• คุณสมบัติพื้นฐานของคอมโพเนนต์
• วิธีสร้างคอมโพเนนต์ใช้เอง
• การใช้งาน css และ Bootstrap 5 framework ใน
React
Messaging app
Since Facebook created and released React, it’s a bit
surprising that its development team don’t use it for
building Messenger. Still, it’s possible to create an instant
messaging app with the usage of React because it acts as
a WebSocket client. It’s responsible for two-way
Social network
This point is highly connected with the previous one
because it’s recommended to build social networks as
Single Page Applications (SPAs). Since the SPA user doesn’t
have to reload the page, i.e. to check new messages, it
strongly improves user experience.
npm
yarn
$ npm start
HTML
JSX
<App className=“App”>This is JSX Element</App>
createElement()
function App(){
const myName = 'Samit Koyom'
return (
<div className="App">
<h1>ชื่อของผมคือ { myName }</h1>
</div>
)
}
function App(){
const mySalary = 24000
const myWifeSalary = 16000
return (
<div className="App">
<h1>รายได้รวมของภรรยา { mySalary + myWifeSalary }</h1>
</div>
)
}
function App(){
return (
<div className="App">
<h1>Live สดถือทะเบียนสมรสไปงานแต่ง</h1>
<h2>ล่าสุดฟ้องศาลแล้ว เรียกเงินจากเจ้าสาว</h2>
</div>
)
}
src/App.css
.myStyle {
color: teal;
font-size: 30px;
border:2px solid #61dafb;
border-radius: 10px;
padding: 10px;
text-align: left;
}
function App(){
return (
<div className="App">
<p className="myStyle">
เธอบัญชีอะไรฉันจะโอน<br />
ถึงหน้าตาของฉันเหมือนกันโจร<br />
เบอร์เธอเบอร์อะไรฉันจะโทร<br />
อย่าทาให้ฉันเหงา เหงา Alone
</p>
</div>
)
}
1. Class Component
2. Function Component