ผู้เขียน หัวข้อ: Basic Laravel5.5 Part1  (อ่าน 7802 ครั้ง)

ออฟไลน์ ผู้ดูแลระบบ

  • Administrator
  • Jr. Member
  • *****
  • กระทู้: 66
  • +0/-0
    • ดูรายละเอียด
Basic Laravel5.5 Part1
« เมื่อ: ตุลาคม 08, 2017, 12:26:32 am »
ติดตั้ง Laravel 5.0.22 ด้วย composer ผ่าน CLI
cd 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 Folder

folder "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/hello

Other 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/hello


View

จากการทดสอบทั้งหมดเรายังไม่ได้สร้าง 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
« แก้ไขครั้งสุดท้าย: ตุลาคม 09, 2017, 03:58:19 pm โดย ผู้ดูแลระบบ »