์ค์ต ๊ฐ์
- ํ๋ก์ ํธ ์ด๊ธฐํ ๋ฐ ์ค์
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค์
- ๋ง์ด๊ทธ๋ ์ด์ ์์ฑ ๋ฐ ์คํ (ํ ์ผ ๋ชฉ๋ก ํ ์ด๋ธ ๋ง๋ค๊ธฐ)
- ๋ชจ๋ธ๊ณผ ์ปจํธ๋กค๋ฌ ์์ฑ
- ๋ผ์ฐํ ๋ฐ ๋ทฐ ํ์ผ ์์ฑ
- CRUD ๊ธฐ๋ฅ ์ถ๊ฐ (ํ ์ผ ์ถ๊ฐ, ์กฐํ, ์์ , ์ญ์ )
1. Laravel ํ๋ก์ ํธ ์์ฑ
ํฐ๋ฏธ๋์์ Laravel ํ๋ก์ ํธ๋ฅผ ์๋ก ์์ฑํฉ๋๋ค.
composer create-project --prefer-dist laravel/laravel="8.*" todo-app
2. MySQL ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค์
MySQL ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฌ์ฉ์ ๊ณ์ ์ ์์ฑํฉ๋๋ค. ํฐ๋ฏธ๋์ด๋ MySQL ํด๋ผ์ด์ธํธ์์ ๋ค์ ๋ช ๋ น์ด๋ฅผ ์คํํ์ธ์.
CREATE DATABASE todo_app;
CREATE USER 'todo_user'@'localhost' IDENTIFIED BY 'password123';
GRANT ALL PRIVILEGES ON todo_app.* TO 'todo_user'@'localhost';
FLUSH PRIVILEGES;
์ด ๋ช ๋ น์ด๋ก todo_app๋ผ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ todo_user๋ผ๋ ์ฌ์ฉ์๋ฅผ ์์ฑํ๊ณ , ๋น๋ฐ๋ฒํธ๋ฅผ password123์ผ๋ก ์ค์ ํฉ๋๋ค.
3. Laravel .env ํ์ผ์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ์ค์
Laravel ํ๋ก์ ํธ ํด๋์ ์๋ .env ํ์ผ์ ์ด๊ณ , ๋ค์ ์ค์ ์ ์ ๋ฐ์ดํธํฉ๋๋ค.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=todo_app
DB_USERNAME=todo_user
DB_PASSWORD=password123
((MySQL ์ ๋ณด์ ๋ง๊ฒ ์์ ํ์ธ์.))
4. ๋ง์ด๊ทธ๋ ์ด์ ์์ฑ: ํ ์ผ ๋ชฉ๋ก ํ ์ด๋ธ ๋ง๋ค๊ธฐ
ํ ์ผ ๋ชฉ๋ก์ ์ ์ฅํ ํ ์ด๋ธ์ ๋ง๋ค๊ธฐ ์ํด ๋ง์ด๊ทธ๋ ์ด์ ํ์ผ์ ์์ฑํฉ๋๋ค.
php artisan make:migration create_tasks_table --create=tasks
์ด ๋ช ๋ น์ด๋ฅผ ์คํํ๋ฉด database/migrations ํด๋์ ์๋ก์ด ๋ง์ด๊ทธ๋ ์ด์ ํ์ผ์ด ์์ฑ๋ฉ๋๋ค.
ํ์ผ์ ์ด์ด ๋ค์๊ณผ ๊ฐ์ด ํ ์ด๋ธ ๊ตฌ์กฐ๋ฅผ ์ ์ํฉ๋๋ค.
// database/migrations/xxxx_xx_xx_xxxxxx_create_tasks_table.php
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->string('title'); // ํ ์ผ ์ ๋ชฉ
$table->text('description'); // ํ ์ผ ์ค๋ช
$table->boolean('completed')->default(false); // ์๋ฃ ์ฌ๋ถ
$table->timestamps(); // ์์ฑ ๋ฐ ์์ ์๊ฐ
});
}โ
์ด์ ๋ง์ด๊ทธ๋ ์ด์ ์ ์คํํ์ฌ tasks ํ ์ด๋ธ์ ์์ฑํฉ๋๋ค.
php artisan migrate
5. ๋ชจ๋ธ ์์ฑ: Task ๋ชจ๋ธ ๋ง๋ค๊ธฐ
Eloquent ๋ชจ๋ธ์ ์ฌ์ฉํด tasks ํ ์ด๋ธ์ ์กฐ์ํฉ๋๋ค. Task ๋ชจ๋ธ์ ์์ฑํฉ๋๋ค.
php artisan make:model Task
app/Models/Task.php ํ์ผ์ด ์์ฑ๋์์ ๊ฒ์ ๋๋ค. ์ฌ๊ธฐ์์ ๋ชจ๋ธ์ด ์ด๋ค ํ๋๋ฅผ ์กฐ์ํ ์ ์๋์ง ์ง์ ํฉ๋๋ค.
// app/Models/Task.php
protected $fillable = ['title', 'description', 'completed'];
6. ์ปจํธ๋กค๋ฌ ์์ฑ ๋ฐ ๋ก์ง ์ถ๊ฐ
ํ ์ผ ๋ชฉ๋ก์ CRUD ๊ธฐ๋ฅ์ ์ฒ๋ฆฌํ ์ปจํธ๋กค๋ฌ๋ฅผ ์์ฑํฉ๋๋ค.
php artisan make:controller TaskController
TaskController.php ํ์ผ์ ์ด๊ณ , ํ ์ผ ๋ชฉ๋ก์ ๊ฐ ๊ธฐ๋ฅ (๋ชฉ๋ก ์กฐํ, ์ถ๊ฐ, ์์ , ์ญ์ )์ ์ํ ๋ฉ์๋๋ฅผ ์ถ๊ฐํฉ๋๋ค.
// app/Http/Controllers/TaskController.php
use App\Models\Task;
use Illuminate\Http\Request;
class TaskController extends Controller
{
// ํ ์ผ ๋ชฉ๋ก ์กฐํ
public function index()
{
$tasks = Task::all();
return view('tasks.index', compact('tasks'));
}
// ํ ์ผ ์ถ๊ฐ ํผ ํ์
public function create()
{
return view('tasks.create');
}
// ํ ์ผ ์ ์ฅ
public function store(Request $request)
{
Task::create($request->all());
return redirect()->route('tasks.index');
}
// ํ ์ผ ์์ ํผ ํ์
public function edit(Task $task)
{
return view('tasks.edit', compact('task'));
}
// ํ ์ผ ์
๋ฐ์ดํธ
public function update(Request $request, Task $task)
{
$task->update($request->all());
return redirect()->route('tasks.index');
}
// ํ ์ผ ์ญ์
public function destroy(Task $task)
{
$task->delete();
return redirect()->route('tasks.index');
}
}
7. ๋ผ์ฐํ ์ค์
routes/web.php ํ์ผ์์ ๋ผ์ฐํ ์ ์ค์ ํ์ฌ ๊ฐ URL์ ๋์ํ๋ ๊ธฐ๋ฅ์ ์ ์ํฉ๋๋ค.
use App\Http\Controllers\TaskController;
Route::resource('tasks', TaskController::class);
Route::resource๋ฅผ ์ฌ์ฉํ๋ฉด index, create, store, edit, update, destroy์ ๊ฐ์ ๊ธฐ๋ณธ CRUD ๊ฒฝ๋ก๊ฐ ์๋์ผ๋ก ์์ฑ๋ฉ๋๋ค.
8. ๋ทฐ ํ์ผ ์์ฑ
ํ ์ผ ๋ชฉ๋ก ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ทฐ ํ์ผ์ ์์ฑํฉ๋๋ค. resources/views/tasks ํด๋๋ฅผ ๋ง๋ค๊ณ ๋ค์๊ณผ ๊ฐ์ ํ์ผ์ ์ถ๊ฐํ์ธ์.
ํ ์ผ ๋ชฉ๋ก ํ์ด์ง (index.blade.php)
<!-- resources/views/tasks/index.blade.php -->
<h1>ํ ์ผ ๋ชฉ๋ก</h1>
<a href="{{ route('tasks.create') }}">์ ํ ์ผ ์ถ๊ฐ</a>
<ul>
@foreach ($tasks as $task)
<li>
{{ $task->title }}
<a href="{{ route('tasks.edit', $task) }}">์์ </a>
<form action="{{ route('tasks.destroy', $task) }}" method="POST" style="display:inline;">
@csrf
@method('DELETE')
<button type="submit">์ญ์ </button>
</form>
</li>
@endforeach
</ul>
์ ํ ์ผ ์ถ๊ฐ ํ์ด์ง (create.blade.php)
<!-- resources/views/tasks/create.blade.php -->
<h1>์ ํ ์ผ ์ถ๊ฐ</h1>
<form action="{{ route('tasks.store') }}" method="POST">
@csrf
<label>์ ๋ชฉ:</label>
<input type="text" name="title">
<label>์ค๋ช
:</label>
<textarea name="description"></textarea>
<button type="submit">์ถ๊ฐ</button>
</form>
ํ ์ผ ์์ ํ์ด์ง (edit.blade.php)
<!-- resources/views/tasks/edit.blade.php -->
<h1>ํ ์ผ ์์ </h1>
<form action="{{ route('tasks.update', $task) }}" method="POST">
@csrf
@method('PUT')
<label>์ ๋ชฉ:</label>
<input type="text" name="title" value="{{ $task->title }}">
<label>์ค๋ช
:</label>
<textarea name="description">{{ $task->description }}</textarea>
<button type="submit">์
๋ฐ์ดํธ</button>
</form>
9. ์ ํ๋ฆฌ์ผ์ด์ ์คํ
์ด์ ์ ํ๋ฆฌ์ผ์ด์ ์ ์คํํ์ฌ ํ ์ผ ๋ชฉ๋ก ์ ํ๋ฆฌ์ผ์ด์ ์ด ์ ์๋ํ๋์ง ํ์ธํด ๋ณด์ธ์.
php artisan serve
๋ธ๋ผ์ฐ์ ์์ http://127.0.0.1:8000/tasks๋ก ์ ์ํ๋ฉด ํ ์ผ ๋ชฉ๋ก ์ ํ๋ฆฌ์ผ์ด์ ์ ํ์ธํ ์ ์์ต๋๋ค.
'๐ป์ปดํจํฐ ๊ธฐ๋ก์ฅ > PHP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ผ๋ผ๋ฒจ(Laravel) ๋ทฐ(View) ์ฌ์ฉํ๊ธฐ (0) | 2024.11.08 |
---|---|
Laravel ๋ผ์ฐํธ(Route)์ ์ปจํธ๋กค๋ฌ(Controller) (0) | 2024.11.07 |
Laravel ๊ฐ๋ฐ ์๋ฒ ์คํํ๊ธฐ (Windows11) (0) | 2024.11.06 |
Laravel 8.0 ํ๋ก์ ํธ ์์ฑํ๊ธฐ(Windows11) (0) | 2024.11.05 |
์ปดํฌ์ (Composer) ์ค์นํ๊ธฐ (Windows11) (0) | 2024.10.31 |