/Hello-Laravel

๐Ÿ‘๐Ÿ‘Lets study Laravel PHP Framework!!๐Ÿ‘๐Ÿ‘

Primary LanguagePHP

Hello-Laravel

๐Ÿ‘๐Ÿ‘Lets study Laravel PHP Framework!!๐Ÿ‘๐Ÿ‘

Index

Routing

Laravel Framework๋Š” Front Controller ๋ฐฉ์‹์˜ ๋ผ์šฐํŒ…์„ ์‚ฌ์šฉํ•œ๋‹ค.

์‚ฌ์šฉ์ž์˜ ๋ชจ๋“  Request๊ฐ€ ๋ผ์šฐํŒ… ํŒŒ์ผ๋กœ ๋„˜์–ด์˜จ ํ›„ ์–ด๋–ค ์š”์ฒญ์ธ์ง€์— ๋”ฐ๋ผ ๋ผ์šฐํŒ… ํŒŒ์ผ์—์„œ ๋‹ค์–‘ํ•œ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์š”์ฒญ๋“ค์„ ์ ์ ˆํ•˜๊ฒŒ ๋ถ„๋ฐฐํ•œ๋‹ค.

Request -> [Routing File] -> ์ž„์˜์˜ ์ปจํŠธ๋กค๋Ÿฌ

# ์ปจํŠธ๋กค๋Ÿฌ๋Š” Laravel์—์„œ ์ œ๊ณตํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋„ ์žˆ์ง€๋งŒ, ์ง์ ‘ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.
# Request๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ๋ผ์šฐํŒ… ํŒŒ์ผ์„ ๋ณด๋ฉด ๋œ๋‹ค๋ผ๊ณ  ์ƒ๊ฐํ•˜์ž!

Routing Manual

Route::get('/' function()
{
    return "H4lo, World!";
});
# '/' URL๋กœ ์ ‘๊ทผํ•˜๋ฉด "H4lo, World!" ๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

# Slim PHP Framework ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์•˜๋‹ค๋ฉด ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ์‰ฌ์šธ ๊ฒƒ!
# Route::[Method]('[Resource', function() {[this is call back function]});
# -> Method : get, post, put, delete
# Method URL์— Request๊ฐ€ ์˜ค๋ฉด fuction์œผ๋กœ ์‹ธ์ง„ Closure๊ฐ€ ๋™์ž‘ํ•œ๋‹ค๋Š” ์˜๋ฏธ!

Route::get('/', 'WelcomeController@index';);
# ์‚ฌ์šฉ์ž๊ฐ€ '/' URL๋กœ ์ ‘๊ทผํ•˜๋ฉด, WelcomeController์˜ index๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
# /routes/web.php

Route::get('/', function () {
    return view('welcome');
});
# view() ๋Š” Helper Function์ด๋‹ค.

return View::make('welcome');
# ์œ„์™€ ๊ฐ™์ด Laravel์—์„œ ์ œ๊ณตํ•˜๋Š” Facade๋ฅผ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
# view()-> ๊นŒ์ง€ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์ฝ”๋“œํžŒํŠธ๊ฐ€ ๋‚˜์™€์„œ Helper Function์„ ๋” ์„ ํ˜ธํ•œ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ,
# "์ฝ”๋“œ ํžŒํŠธ"์— ๋Œ€ํ•ด ์•Œ์•„๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋‹ค!
Route::get('/', function () {
    return abort(503);
});

# php artisan down ๋ช…๋ น์œผ๋กœ ์œ ์ง€๋ณด์ˆ˜ ์ƒํƒœ๋กœ ์ „ํ™˜ํ•˜๋ฉด, ์œ„์™€ ๊ฐ™์€ 503 Error๋ฅผ ๋ทฐ๋กœ์จ ๋ณด์—ฌ์ค€๋‹ค.
# php artisan up ๋ช…๋ น์œผ๋กœ ์„œ๋น„์Šค ์ƒํƒœ๋กœ ๋ณต๊ท€ํ•  ์ˆ˜ ์žˆ๋‹ค.
# php artisan suspend ๋ช…๋ น์€ ์„œ๋น„์Šค๋ฅผ ์™„์ „ํžˆ ์ข…๋ฃŒ์‹œํ‚จ๋‹ค.

Data Binding in view

<p>
    {{ $greeting }} {{ $name ?? '' }}. Nice to meet you~~
</p>

# resources/views/ ๊ฒฝ๋กœ์— hello.blade.php ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์˜€์Œ.
# {{ }} ์€ ๋ผ๋ผ๋ฒจ์˜ ํ…œํ”Œ๋ฆฟ ์—”์ง„์ธ ๋ธ”๋ ˆ์ด๋“œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” String interpolation ๋ฌธ๋ฒ•์ด๋‹ค.
# PHP๋กœ ์น˜๋ฉด, ๋ทฐ ๋‚ด์—์„œ <?= ?> ๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•ด์ค€๋‹ค.

# ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ทฐ์— ๋ฐ”์ธ๋”ฉ์‹œํ‚ฌ ๋•Œ์—๋Š” {{ }} ๋Œ€์‹  {!! !!} ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
# {{ $name ?? '' }} ์—์„œ ??๋Š” PHP7 ์ดํ›„๋ถ€ํ„ฐ ๋‚˜์˜จ ๋ฌธ๋ฒ•์œผ๋กœ, $name์— ๊ฐ’์ด ์กด์žฌํ•˜๋ฉด $name์„,
# ์•„๋‹ˆ๋ผ๋ฉด ๊ณต๋ฐฑ์„ ์‚ฌ์šฉํ•˜๋Š” ์‚ผํ•ญ์—ฐ์‚ฐ์ž์˜ ์—ญํ• ์„ ํ•œ๋‹ค. PHP5.3 ์—์„œ๋Š” ?:๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์—ˆ๋‹ค.
# or๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ๋„ ์žˆ์œผ๋‚˜ or๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ name ๊ฐ’ ๋Œ€์‹  1์ด ์ถœ๋ ฅ๋œ๋‹ค.

๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•˜๋Š” ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

with() ๋ฉ”์†Œ๋“œ๋กœ ๋ทฐ์— ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•, view()์˜ 2๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ธฐ๋Š” ๋ฐฉ๋ฒ• ๋“ฑ์ด ์žˆ๋Š”๋ฐ,

์‹ค์ „์—์„œ๋Š” PHP ๋‚ด์žฅํ•จ์ˆ˜์ธ compact(mixed $varname)์™€ ์กฐํ•ฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ์„ ์ง„ํ–‰ํ•˜๋Š” ํŽธ์ด๋‹ค.

compact(mixed $varname)

compact ํ•จ์ˆ˜๋Š” ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฐ๊ด€๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜์ด๋‹ค.

๋ณ€์ˆ˜๋ช…์€ Key ๊ฐ’์œผ๋กœ, ๋ณ€์ˆ˜๊ฐ’์€ ๋ฐฐ์—ด๊ฐ’์œผ๋กœ ๋ฐ”๋€๋‹ค.

๋ฐ˜๋Œ€๋กœ ์—ฐ๊ด€๋ฐฐ์—ด์„ ๋ณ€์ˆ˜๋กœ ์ „ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋Š” extract() ์ด๋‹ค.

Route::get('/hello', function () {
    $greeting = '์•ˆ๋…•ํ•˜์„ธ์š”';
    $name = 'h4lo';

    return view('hello', compact('greeting', 'name'));
});

routes/web.php ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ผ์šฐํŒ… ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

hello.blade.php์— view ๋ฉ”์†Œ๋“œ์˜ ๋‘๋ฒˆ์งธ ์ธ์ž์—์„œ compact ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์ง„ ์—ฐ๊ด€๋ฐฐ์—ด์„ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์œผ๋กœ ๋„˜๊ฒจ, html ์ฝ”๋“œ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.

[๋ชฉ์ฐจ๋กœ]

Blade 101

Laravel Template Engine, Blade

๋ธ”๋ ˆ์ด๋“œ ๋ฌธ๋ฒ•์€ ๋ธ”๋ ˆ์ด๋“œ ์—”์ง„์— ์˜ํ•ด PHP ์ฝ”๋“œ๋กœ ์ปดํŒŒ์ผ๋œ๋‹ค.

{{ }} String interpolation

<html>
{{ $example }}  # ๋ผ์šฐํŒ…์—์„œ ๋ฆฌํ„ดํ•œ $example ๋ณ€์ˆ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ธ๋”ฉ ์‹œ์ผœ์ค€๋‹ค.
<html>

# ์ถœ๋ ฅ : Test is good!
---

Route::get('/', function ()
{
    $example = "Test is good!"
    
    return view('exam', compact('example'));
});

{{-- --}} Comment

{{-- count(range(1, 10)) --}}  # ํ•จ์ˆ˜๊ฐ€ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š” ๋ฏธ์ถœ๋ ฅ ์ฝ”๋“œ.

<!-- {{ count(range(1, 10)) }} -->
# ํŽ˜์ด์ง€ ์†Œ์Šค๋ณด๊ธฐ๋ฅผ ํ•˜๋ฉด count(range(1, 10)) ํ•จ์ˆ˜๋Š” ์‹คํ–‰๋˜์–ด <!-- 10 --> ๊ณผ ๊ฐ™์ด ๊ฐ’์ด ๋ฐ”๋€ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

@foreach

<ul>
    @foreach($items as $item)
        <li>{{ $item }}</li>
    @endforeach
</ul>

# blade.php ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด html ์ฝ”๋“œ ์ƒ์— @foreach๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด, ๋ฆฌํ„ด๋ฐ›์€ ์—ฐ๊ด€๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋งŒํผ foreach ๋ฌธ์ด ๋ฐ˜๋ณต๋œ๋‹ค.

@for๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ!

@if

@if(isset($test))
    <p>H4lo!!</p>
@else
    <p>Null!!</p>
@endif

# if ๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
# @elseif ๋„ ๊ฐ€๋Šฅ
# @unless (== if(!)) ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ 

๋ฆฌํ„ด๋ฐ›์€ ๋ฐ์ดํ„ฐ๋กœ ArrayAccess๋ฅผ ํ•  ์ˆ˜ ์—†๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•˜์ง€?

@forelse ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž!

@forelse

forelse๋Š” foreach์™€ if๋ฅผ ์„ž์€ ๋Š๋‚Œ!

๋ฆฌํ„ด๋ฐ›์€ ๋ณ€์ˆ˜์— ๊ฐ’์ด ์žˆ๊ณ  ArrayAccess๊ฐ€ ๊ฐ€๋Šฅํ•˜๋ฉด, @forelse ๋ฌธ์œผ๋กœ ๋“ค์–ด๊ฐ€๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด @empty ๊ฒฝ๋กœ๋ฅผ ํƒ€๊ฒŒ ๋œ๋‹ค.

@forelse($items as $item)
    <p>{{ $item }}</p>
@empty
    <p>Empty..</p>
@endforelse

[๋ชฉ์ฐจ๋กœ]

Blade 201

Master Layout in Laravel

ํŽ˜์ด์ง€๋งˆ๋‹ค ๋ฐ˜๋ณต๋˜๋Š” Header, Section, Footer๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ ๋„ฃ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” Blade ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž~

@yield, @extends, @section

resources/views/master.blade.php

<!DOCTYPE HTML>
<html lang='ko'>
<head>
    <meta charset='utf-8'>
    <title>Blade 201 example</title>
</head>
<body>
    @yield('style')
    @yield('content')
    @yield('script')
</body>
</html>
resources/views/blade201.blade.php

@extends('master')

@section('style')
    <style>
        body {background : lightskyblue;}
    </style>
@endsection

@section('content')
    H4lo~~
@endsection

@section('script')
    <script>
        alert('H4lo~~ Nice to meet you~~')
    </script>
@endsection

@include

Footer ํƒœ๊ทธ๋ฅผ include ํ•ด๋ณด์ž!

resources/views/footer.blade.php

<footer>
    <p>This is footer area</p>
</footer>
resources/views/master.blade.php

@yield('style')
@yield('content')
@yield('script')

@include('footer') # footer

@extends๋กœ ๋ช…์‹œํ•ด๋†“์€ ๊ฒฝ๋กœ์˜ ํŒŒ์ผ์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ 500 ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ!!

[๋ชฉ์ฐจ๋กœ]

DB Query with Artisan Tinker

select

$ php artisan tinker
>>> DB::select('select * from [Table Name]')
=> [
     {#(id value)
       +"Key1": value,
       +"Key2": value,
       +"Key3": value,
       +"Key4": value.
     },
   ]

insert

$ php artisan tinker
>>> DB::insert('insert into [Table Name] (column1, column2) values (?, ?)', [value1, value2]);
=> true

Laravel์€ PDO๋ฅผ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ์€ ์ง์ ‘ ๊ฐ’์„ ๋Œ€์ž…ํ•˜์ง€ ์•Š๊ณ , ?๋ฅผ ์ด์šฉํ•œ ๋’ค, ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ธ๋”ฉ ์‹œ์ผœ์ค€๋‹ค.

update

$ php artisan tinker
>>> DB::update('update [Table Name] set (column1, column2) values (?, ?)', [value1, value2]);
=> 1

etc..

SQLSTATE[HY000] [2002] Connection refused Error

Laravel ๋‚ด์˜ .env ํŒŒ์ผ๊ณผ config/database.php ํŒŒ์ผ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ •๋ณด๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ๋ฌธ์ œ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๋ฅผ ๋ณด์ด๊ณ  Artisan Tinker ๋‚ด์—์„œ ์ •์ƒ์ ์œผ๋กœ DB ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ config/database.php ํŒŒ์ผ ๋‚ด์˜ mysql port๋Š” 33060์œผ๋กœ ์„ค์ •์ด ๋˜์–ด์žˆ์—ˆ๋˜ ๊ฒƒ์œผ๋กœ ๊ธฐ์–ต๋œ๋‹ค.

Homestead ์—์„œ mysql port๋ฅผ 33060์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ๋Š” ํ•˜๋‚˜, ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” config.database.php์˜ mysql port ์„ค์ •๋ž€์— 'port' => env('DB_PORT', '3306') ์™€ ๊ฐ™์ด ์„ค์ •ํ•ด์ค€ ํ›„ php artisan cache:clear ๋กœ ์บ์‹œ๋ฅผ ์ดˆ๊ธฐํ™” ์‹œ์ผœ์ฃผ๋ฉด ์ •์ƒ์ ์œผ๋กœ tinker ๋‚ด์—์„œ DB ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

[๋ชฉ์ฐจ๋กœ]

Query Builder