Difference between revisions of "Basic Laravel Training (for CV)"
Jump to navigation
Jump to search
| Line 62: | Line 62: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# ลองสร้าง method index | # ลองสร้าง method index | ||
| − | + | #: <syntaxhighlight lang="php" line> | |
| + | public function index(){ | ||
| + | $menu = array("Home", "My course", "Setting"); | ||
| + | return $menu; | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | # เพิ่ม Route ใน <code>routes/web.php</code> | ||
| + | #: <syntaxhighlight lang="php" line> | ||
| + | Route::get('home','page_controller@index'); | ||
| + | </syntaxhighlight> | ||
| + | # ลองทดสอบเรียก url | ||
| + | #: <pre>http://localhost:8000/home</pre> | ||
| + | # ปกติแล้วสิ่งที่ controller คืนมักจะไม่ใช่ array หรือ object ธรรมดา ยกเว้นกรณีที่กำลังเขียน api .. หากเป็นกรณี webpage มักจะคืนเป็น view | ||
| + | # ลองสร้าง view (การสร้าง view จะไม่สามารถเรียกผ่าน artisan ได้ จะต้องสร้างด้วยตัวเอง ใน <code>resources/views</code> โดยให้สร้าง view ชื่อว่า <code>home.blade.php</code> | ||
| + | # ใน controller method index จะเปลี่ยนแปลงการ return ให้ return view แทน | ||
| + | #: <syntaxhighlight lang="php" line> | ||
| + | public function index(){ | ||
| + | $menu = array("Home", "My course", "Setting"); | ||
| + | return view("home")->with('menu',$menu); | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | # ใน <code>home.blade.php</code> ให้ลองเขียน html เพื่อแสดง menu เป็น unordered list | ||
| + | #: <syntaxhighlight lang="html" line> | ||
| + | <!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>Document</title> | ||
| + | </head> | ||
| + | <body> | ||
| + | <ul> | ||
| + | @foreach($menu as $eachmenu) | ||
| + | <li>{{$eachmenu}}</li> | ||
| + | @endforeach | ||
| + | </ul> | ||
| + | </body> | ||
| + | </html> | ||
| + | </syntaxhighlight> | ||
==== View (Blade Template) ==== | ==== View (Blade Template) ==== | ||
Revision as of 23:21, 7 March 2019
Contents
Reference
- Laravel Documentation
https://laravel.com/docs/5.8
Installation
Requirements
- PHP >= 7.1.3
- OpenSSL PHP Extension
- PDO PHP Extension
- Mbstring PHP Extension
- Tokenizer PHP Extension
- XML PHP Extension
- Ctype PHP Extension
- JSON PHP Extension
- BCMath PHP Extension
Steps
- Install LAMP Stack (ie. XAMPP) อย่าลืมจำ mysql root password!!
- Install Composer
- https://getcomposer.org/doc/00-intro.md#installation-linux-unix-macos
- หมายเหตุ: เวลาเรียกใช้งาน composer อาจจะต้องใช้
./composer.pharแทนที่composerถ้าทำการ Install composer เป็น local
- สร้าง Laravel Project โดยใช้ composer
composer create-project --prefer-dist laravel/laravel PROJECT_NAME
- Run Local Development Server
php artisan serve # for running on localhost:8000
- or
php artisan serve --port=8888 # for running on localhost:8888
Basic Tutorial
Important directory / file
appWhere the models areHttpControllersMiddleware
configdatabasepublicWhere the public files (ie. css, js)resourcesviews
routesapi.phpweb.php
vendorWhere the additional libraries store.envall environment variables (ie. DB username/password)
Intro
Controller & Route
- Let's make a controller: page_controller
php artisan make:controller CONTROLLER_NAME
- จะมีไฟล์
app/Http/Controllers/page_controller.phpปรากฎ
- ในไฟล์จะมี code เหล่านี้
1 <?php 2 3 namespace App\Http\Controllers; 4 5 use Illuminate\Http\Request; 6 7 class page_controller extends Controller 8 { 9 // 10 }
- ลองสร้าง method index
1 public function index(){ 2 $menu = array("Home", "My course", "Setting"); 3 return $menu; 4 }
- เพิ่ม Route ใน
routes/web.php1 Route::get('home','page_controller@index');
- ลองทดสอบเรียก url
http://localhost:8000/home
- ปกติแล้วสิ่งที่ controller คืนมักจะไม่ใช่ array หรือ object ธรรมดา ยกเว้นกรณีที่กำลังเขียน api .. หากเป็นกรณี webpage มักจะคืนเป็น view
- ลองสร้าง view (การสร้าง view จะไม่สามารถเรียกผ่าน artisan ได้ จะต้องสร้างด้วยตัวเอง ใน
resources/viewsโดยให้สร้าง view ชื่อว่าhome.blade.php - ใน controller method index จะเปลี่ยนแปลงการ return ให้ return view แทน
1 public function index(){ 2 $menu = array("Home", "My course", "Setting"); 3 return view("home")->with('menu',$menu); 4 }
- ใน
home.blade.phpให้ลองเขียน html เพื่อแสดง menu เป็น unordered list1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 7 <title>Document</title> 8 </head> 9 <body> 10 <ul> 11 @foreach($menu as $eachmenu) 12 <li>{{$eachmenu}}</li> 13 @endforeach 14 </ul> 15 </body> 16 </html>
View (Blade Template)
Database
Model & DB
- php artisan make:model MODEL_NAME --migration
- php artisan make:migration MIGRATION_NAME
Eloquent
Handle CRUD
- php artisan make:controller CONTROLLER_NAME --resource
- method: get, post, patch, delete
- Route::resource('ROUTE', 'CONTROLLER');