Difference between revisions of "Basic Laravel Training (for CV)"

From Ta Wiki
Jump to navigation Jump to search
Line 15: Line 15:
 
* BCMath PHP Extension
 
* BCMath PHP Extension
 
=== Steps ===
 
=== Steps ===
# Install LAMP Stack (ie. XAMPP)
+
# Install LAMP Stack (ie. XAMPP) อย่าลืมจำ mysql root password!!
 
# Install Composer
 
# Install Composer
 
#: https://getcomposer.org/doc/00-intro.md#installation-linux-unix-macos
 
#: https://getcomposer.org/doc/00-intro.md#installation-linux-unix-macos
 
#: หมายเหตุ: เวลาเรียกใช้งาน composer อาจจะต้องใช้ <code>./composer.phar</code> แทนที่ <code>composer</code> ถ้าทำการ Install composer เป็น local
 
#: หมายเหตุ: เวลาเรียกใช้งาน composer อาจจะต้องใช้ <code>./composer.phar</code> แทนที่ <code>composer</code> ถ้าทำการ Install composer เป็น local
# Create new Laravel Project
+
# สร้าง Laravel Project โดยใช้ composer
 
#: <pre>composer create-project --prefer-dist laravel/laravel PROJECT_NAME </pre>
 
#: <pre>composer create-project --prefer-dist laravel/laravel PROJECT_NAME </pre>
 
# Run Local Development Server
 
# Run Local Development Server
Line 28: Line 28:
 
== Basic Tutorial ==
 
== Basic Tutorial ==
 
=== Important directory / file ===
 
=== Important directory / file ===
* <code>app</code>: Where the models are
+
* <code>app</code> Where the models are
 
** <code>Http</code>
 
** <code>Http</code>
 
*** <code>Controllers</code>
 
*** <code>Controllers</code>
Line 34: Line 34:
 
* <code>config</code>
 
* <code>config</code>
 
* <code>database</code>
 
* <code>database</code>
* <code>public</code>: Where the public files (ie. css, js)
+
* <code>public</code> Where the public files (ie. css, js)
 
* <code>resources</code>
 
* <code>resources</code>
 
** <code>views</code>
 
** <code>views</code>
Line 40: Line 40:
 
** <code>api.php</code>
 
** <code>api.php</code>
 
** <code>web.php</code>
 
** <code>web.php</code>
* <code>vendor</code>: Where the additional libraries store
+
* <code>vendor</code> Where the additional libraries store
* <code>.env</code>: all environment variables (ie. DB username/password)
+
* <code>.env</code> all environment variables (ie. DB username/password)
 +
 
 +
=== Intro ===
 +
==== Controller & Route ====
 +
# Let's make a controller: page_controller
 +
#: <pre>php artisan make:controller CONTROLLER_NAME</pre>
 +
#: จะมีไฟล์ <code>app/Http/Controllers/page_controller.php</code> ปรากฎ
 +
# ในไฟล์จะมี code เหล่านี้
 +
#: <syntaxhighlight lang="php" line>
 +
<?php
 +
 
 +
namespace App\Http\Controllers;
 +
 
 +
use Illuminate\Http\Request;
 +
 
 +
class page_controller extends Controller
 +
{
 +
    //
 +
}
 +
</syntaxhighlight>
 +
# ลองสร้าง method index
 +
 
 +
==== 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');
 +
 
 +
=== Authentication ===
 +
==== User System ====
 +
 
 +
==== Access Control ====

Revision as of 19:20, 7 March 2019

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

  1. Install LAMP Stack (ie. XAMPP) อย่าลืมจำ mysql root password!!
  2. Install Composer
    https://getcomposer.org/doc/00-intro.md#installation-linux-unix-macos
    หมายเหตุ: เวลาเรียกใช้งาน composer อาจจะต้องใช้ ./composer.phar แทนที่ composer ถ้าทำการ Install composer เป็น local
  3. สร้าง Laravel Project โดยใช้ composer
    composer create-project --prefer-dist laravel/laravel PROJECT_NAME 
  4. 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

  • app Where the models are
    • Http
      • Controllers
      • Middleware
  • config
  • database
  • public Where the public files (ie. css, js)
  • resources
    • views
  • routes
    • api.php
    • web.php
  • vendor Where the additional libraries store
  • .env all environment variables (ie. DB username/password)

Intro

Controller & Route

  1. Let's make a controller: page_controller
    php artisan make:controller CONTROLLER_NAME
    จะมีไฟล์ app/Http/Controllers/page_controller.php ปรากฎ
  2. ในไฟล์จะมี 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 }
    
  3. ลองสร้าง method index

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');

Authentication

User System

Access Control