ติดตั้ง Laravel 5.0.22 ด้วย composer ผ่าน CLIcd c:\xampp\htdocs
composer create-project laravel/laravel:5.0.22 --prefer-dist laravelติดตั้ง Laravel 5.5 composer global require "laravel/installer"
สร้าง project ใหม่
composer create-project --prefer-dist laravel/laravel laravel
การทดสอบ laravelใน xampp
http://localhost/laravel/publicในเครื่องมือที่มาพร้อม laravel
ใน command
cd laravel
สั่ง
php artisan serveทดสอบที่
http://localhost:8000แต่ถ้าต้องการเปลี่ยน port
php artisan serve --port=8080 ทดสอบที่ http://localhost:8000โครงสร้าง Application Folderfolder "app" เก็บ code หลักของ application ของเรา
folder "bootstrap" เก็บ file bootstrap framework และ configure autoloading
folder "config" เก็บ file config ต่างๆ
folder "database" เก็บ file migration and seed
folder "public" เก็บพวก front controller,CSS,JavaScript,Images และ public file
folder "resources" เก็บ views,raw assets (LESS ,Sass,CoffeeScript) and language files
folder "storage" เก็บ file-base sessions,file caches,compiled blade template
folder "test" เก็บ automated tests
folder "vendor" เก็บ package ของ composer dependencies
folder "routes" แยกมาเองต่างหากใน version 5.5
Directory ที่เราใช้บ่อยroutes //ใน laravel 5.5 ได้แยก route ออกมาเป็นอีก folder เองอีกต่างหาก
Controller /app/Http/Controllers/
View /resources/views/
Model /app/ ใน laravel ไม่ได้เก็บ model แยก folder แต่จะรวมไว้ใน app
Migration /database/migrations/ เก็บ table schema
Application Configuration/config/app.php
'timezone' => 'Asia/Bangkok',/.env
APP_ENV=local #local or production
APP_DEBUG=true #false for production สำหรับเครื่องพัฒนาควรแสดง debug แต่สำหรับเครื่องใช้งานจริงไม่ควรให้แสดง
APP_KEY=Random
Routingเป็นตัวที่กำหนดว่า user ข้างนอกสามารถเข้ามาใช้ uri อะไรได้บ้าง มี macanisiom ป้องกัน
Basic Routingจะมีการตรวจสอบว่า URL Request จะทำงานที่ Cotroller ใด method ใด ให้ไปดูที่ rout file
route file อยู่ที่ /app/Http/routes.php ส่วน laravel 5.5 อยู่ที่ folder /routes/web.php
ทดสอบการเขียน routing ใน file /routes/web.php
Route::get('hello',function(){
return 'Hello Laravel';
});เรียกผ่าน url :
http://localhost:8000/helloOther Basic Routingใน route ให้ใช้ดังนี้
Route::post('hello/store',function(){ //basic function ที่เราเรียก function ที่ไป map กับ model
//.......
});
Route::put('hello/update',function(){
//.......
});
Route::patch('hello/update',function(){
//.......
});
Route::delete('hello/destroy',function(){
//.......
});
Basic Routing for Multiple Verb
มี match method ที่เข้ามาเป็น get หรือ post ก็ได้ถ้าเรียกhello ก็สามารถใช้ get หรือ post ก็ได้
Route::match(['get','post'],'hello',function(){
//.......
});any คือเมื่อเข้ามาเป็น get,post,put,patch อะไรก็ตามถ้าเรียกเข้ามาด้วย url ตัวอย่าง hello/edit ก็จะเข้าใช้งาน function นี้เสมอ
Route::any('hello/edit',function(){
//.......
});Route to Controllerการใช้งานจริงๆแล้วเราใช้เรียกใช้ route ไป Controller
ตัวอย่างเช่น
Route::get('/user', 'UsersController@index');สร้าง UsersController
ที่ root directory พิมพ์:
php artisan make:controller UsersControllerที่ file app/Http/Controllers/UsersController.php เพิ่ม function index
public function index(){
echo 'Test ok';
}ทดสอบ route ผ่าน url
http://localhost:8000/user ก็จะแสดงข้อความ "Test ok"
ถ้าต้องการให้ route ส่ง parameter ไปด้วยใช้รูปแบบนี้Route::get('welcome/page/{id}','WelcomeController@page');ใน UsersController.php เพิ่ม function page เพื่อรับ parameter
public function page($id){
return 'Hi id ='.$id;
}ทดสอบ
http://localhost/laravel/public/user/page/3ถ้าเราไม่ใส่ค่า parameter นั้นจะทำให้error ดังนั้นถ้าเราต้องการให้มันไม่ต้อง check ว่าต้องมีหรือไม่มี parameter ก็ได้ให้แก้ที่ routeเป็น
Route::get('welcome/page/{id?}','WelcomeController@page'); // เติม ? ท้าย idและแก้ function page
public function page($id=null){
return 'Hi id ='.$id;
}Route Parameter –RegExpParamsการ route แบบตรวจสอบเงื่อนไขของ parameter ด้วย
Route::get('/user/page/{id?}/{title?}','UsersController@page')
->where(['id'=>'[0-9]+', 'title'=>'[a-zA-Z]+']);แก้ function
public function page($id=null,$title=null){
return 'Hi id ='.$id."title =".$title;
}ทดสอบ
http://localhost/laravel/public/user/page/30/testtitleแต่ถ้าเราแทนค่า 30 เป็น ตัวอักษร อื่นมันจะฟ้อง error
http://localhost/laravel/public/user/page/30/testtitle
Route Controllers // พบว่าใช้งานไม่ได้ใน laravel 5.5จะเห็นว่าถ้าเรามี funcion เยอะเราก็จะต้องกำหนด route file เยอะตามใน laravel จึงมีการ route ทั้ง controller ด้วยคำสั่ง
Route::controller('user', 'UsersController');//user คือ uri ตั้งต้นสำหรับการ route
การสร้าง Controllerใช้คำสั่งใน command
php artisan make:controller <namecontroller> --plain // แบบ --plain คือแบบไม่ต้องขึ้น function default
ใน route
Route::get('hello', 'HelloController@index');ใน HelloController
public function index(){
return "Hello from HelloController";
}ทดสอบ
http://localhost/laravel/public/helloViewจากการทดสอบทั้งหมดเรายังไม่ได้สร้าง view
สร้าง view ธรรมดาให้สร้าง file View ชื่อตรงกัน
ดังตัวอย่าง
route file
/routes/web.php
Route::get('hello', 'HelloController@index');HelloController file
\app\Http\Controllers\HelloController.php
public function index(){
return view('hello.index');
}Viewfile
resources\views\hello\index.php //ยังไม่ใช้ bald file
<!Doctype html>
<html>
<head>
<title>Laravel5–Home Page</title>
</head>
<body>
<h1>Welcome to Laravel5</h1>
<p>This is Hello Page</p>
</body>
</html>ทดสอบ
http://localhost/laravel/public/hello
วิธีส่งผ่าน parameter ทาง view มีหลายวิธี
วิธีส่งผ่านด้วย with
แก้ ที่ Controller //ส่งแบบ parameter แบบธรรมดา
public function index(){
return view('hello.index')
->with('title','Laravel 505 Training')
->with('subtitle','An introductin to Laravel 5.5');
}ถ้าส่งแบบ Array
public function index(){
return view('hello.index')
->with([
'title'=>'Laravel 505 Training',
'subtitle'=>'An introductin to Laravel 5.5'
]);
}หรือ
public function index(){
$title = 'Laravel 505 Training';
$subtitle='From basic to expert';
return view('hello.index')
->with([
'title'=>$title,
'subtitle'=>$subtitle
]);
}หรือ
public function index(){
$title = 'Laravel 505 Training';
$subtitle='From basic to expert';
return view('hello.index')
->withTitle($subtitle)
->withSubtitle($subtitle);
}ส่วน View ให้รับค่าดังนี้
<!Doctype html>
<html>
<head>
<title>Laravel5–Home Page</title>
</head>
<body>
<h1><?=$title?></h1>
<p><?=$subtitle?></p>
</body>
</html>Displaying Data on View with Bladeถ้าจะใช้ Blade เราใช้ {{}} duble bladese แทน <?php ?>
<!Doctype html>
<html>
<head>
<title>Laravel5–Home Page</title>
</head>
<body>
<h1>{{$title}}</h1>
<p>{{$subtitle}}</p>
</body>
</html>อ้างอิงhttps://www.youtube.com/watch?v=C3vmNI5Whus&index=1&list=PLtM3znnbMbVUCSplQZ4Wl5KwOj6Inz__n