PHP
php CRUD와 모듈화
식혜야
2023. 12. 20. 18:36
CRUD
index.php
<?php
function print_title(){
if(isset($_GET['id'])){
echo $_GET['id'];
} else {
echo "Welcome";
}
}
function print_description(){
if(isset($_GET['id'])){
echo file_get_contents("data/".$_GET['id']);
} else {
echo "Hello, PHP";
}
}
function print_list(){
$list = scandir('./data');
$i = 0;
while($i < count($list)){
if($list[$i] != '.') {
if($list[$i] != '..') {
echo "<li><a href=\"index.php?id=$list[$i]\">$list[$i]</a></li>\n";
}
}
$i = $i + 1;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
<?php
print_title();
?>
</title>
</head>
<body>
<h1><a href="index.php">WEB</a></h1>
<ol>
<?php
print_list();
?>
</ol>
<a href="create.php">create</a>
<?php if(isset($_GET['id'])) { ?>
<a href="update.php?id=<?=$_GET['id']?>">update</a>
<form action="delete_process.php" method="post">
<input type="hidden" name="id" value="<?=$_GET['id']?>">
<input type="submit" value="delete">
</form>
<?php } ?>
<h2>
<?php
print_title();
?>
</h2>
<?php
print_description();
?>
</body>
</html>
create.php
<?php
function print_title(){
if(isset($_GET['id'])){
echo $_GET['id'];
} else {
echo "Welcome";
}
}
function print_description(){
if(isset($_GET['id'])){
echo file_get_contents("data/".$_GET['id']);
} else {
echo "Hello, PHP";
}
}
function print_list(){
$list = scandir('./data');
$i = 0;
while($i < count($list)){
if($list[$i] != '.') {
if($list[$i] != '..') {
echo "<li><a href=\"index.php?id=$list[$i]\">$list[$i]</a></li>\n";
}
}
$i = $i + 1;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
<?php
print_title();
?>
</title>
</head>
<body>
<h1><a href="index.php">WEB</a></h1>
<ol>
<?php
print_list();
?>
</ol>
<a href="create.php">create</a>
<form action="create_process.php" method="post">
<p>
<input type="text" name="title" placeholder="Title">
</p>
<p>
<textarea name="description" placeholder="Description"></textarea>
</p>
<p>
<input type="submit">
</p>
</form>
</body>
</html>
create_process.php
<?php
file_put_contents('data/'.$_POST['title'], $_POST['description']);
header('Location: /index.php?id='.$_POST['title']);
?>
update.php
<?php
function print_title(){
if(isset($_GET['id'])){
echo $_GET['id'];
} else {
echo "Welcome";
}
}
function print_description(){
if(isset($_GET['id'])){
echo file_get_contents("data/".$_GET['id']);
} else {
echo "Hello, PHP";
}
}
function print_list(){
$list = scandir('./data');
$i = 0;
while($i < count($list)){
if($list[$i] != '.') {
if($list[$i] != '..') {
echo "<li><a href=\"index.php?id=$list[$i]\">$list[$i]</a></li>\n";
}
}
$i = $i + 1;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
<?php
print_title();
?>
</title>
</head>
<body>
<h1><a href="index.php">WEB</a></h1>
<ol>
<?php
print_list();
?>
</ol>
<a href="create.php">create</a>
<?php if(isset($_GET['id'])) { ?>
<a href="update.php?id=<?=$_GET['id']?>">update</a>
<?php } ?>
<h2>
<form action="update_process.php" method="post">
<input type="hidden" name="old_title" value="<?=$_GET['id']?>">
<p>
<input type="text" name="title" placeholder="Title" value="<?php print_title(); ?>">
</p>
<p>
<textarea name="description" placeholder="Description"><?php print_description(); ?></textarea>
</p>
<p>
<input type="submit">
</p>
</form>
</body>
</html>
update_process.php
<?php
rename('data/'.$_POST['old_title'], 'data/'.$_POST['title']);
file_put_contents('data/'.$_POST['title'], $_POST['description']);
header('Location: /index.php?id='.$_POST['title']);
?>
delete_process.php
<?php
unlink('data/'.$_POST['id']);
header('Location: /index.php');
?>
모듈화 - require(), require_once()
위의 CRUD 코드는 중복하는 코드가 많아 비효율적이다. 중복되는 코드를 파일로 모듈화 하는 리팩토링 과정을 require()
함수와, require_once()
함수를 통해 진행해 보자.
*외부 파일을 포함하는 함수 4가지 정리
https://www.codingfactory.net/10121
PHP / include(), include_once(), require(), require_once() / 외부 파일 포함하는 함수
여러 파일에 공통적으로 사용하는 코드는 별도의 파일로 만든 후 각 파일에서 불러오는 것이 좋습니다. 코드의 양이 줄어들고, 수정이 용이하기 때문입니다. 외부 파일을 포함하는 함수는 네 가
www.codingfactory.net
lib/print.php
<?php
function print_title(){
if(isset($_GET['id'])){
echo $_GET['id'];
} else {
echo "Welcome";
}
}
function print_description(){
if(isset($_GET['id'])){
echo file_get_contents("data/".$_GET['id']);
} else {
echo "Hello, PHP";
}
}
function print_list(){
$list = scandir('./data');
$i = 0;
while($i < count($list)){
if($list[$i] != '.') {
if($list[$i] != '..') {
echo "<li><a href=\"index.php?id=$list[$i]\">$list[$i]</a></li>\n";
}
}
$i = $i + 1;
}
}
?>
view/top.php
<?php
require_once('lib/print.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
<?php
print_title();
?>
</title>
</head>
<body>
<h1><a href="index.php">WEB</a></h1>
<ol>
<?php
print_list();
?>
</ol>
view/bottom.php
</body>
</html>
index.php
<?php
require_once('lib/print.php');
require_once('view/top.php');
?>
<a href="create.php">create</a>
<?php if(isset($_GET['id'])) { ?>
<a href="update.php?id=<?=$_GET['id']?>">update</a>
<form action="delete_process.php" method="post">
<input type="hidden" name="id" value="<?=$_GET['id']?>">
<input type="submit" value="delete">
</form>
<?php } ?>
<h2>
<?php
print_title();
?>
</h2>
<?php
print_description();
?>
<?php
require_once('view/bottom.php');
?>
create.php
<?php
require('lib/print.php');
require('view/top.php');
?>
<a href="create.php">create</a>
<form action="create_process.php" method="post">
<p>
<input type="text" name="title" placeholder="Title">
</p>
<p>
<textarea name="description" placeholder="Description"></textarea>
</p>
<p>
<input type="submit">
</p>
</form>
<?php
require('view/bottom.php');
?>
update.php
<?php
require('lib/print.php');
require('view/top.php');
?>
<a href="create.php">create</a>
<?php if(isset($_GET['id'])) { ?>
<a href="update.php?id=<?=$_GET['id']?>">update</a>
<?php } ?>
<h2>
<form action="update_process.php" method="post">
<input type="hidden" name="old_title" value="<?=$_GET['id']?>">
<p>
<input type="text" name="title" placeholder="Title" value="<?php print_title(); ?>">
</p>
<p>
<textarea name="description" placeholder="Description"><?php print_description(); ?></textarea>
</p>
<p>
<input type="submit">
</p>
</form>
<?php
require('view/bottom.php');
?>
References
https://opentutorials.org/module/3266
WEB2 - PHP
수업소개 이 수업은 웹사이트를 만들어가면서 PHP의 문법과 웹애플리케이션의 제작법을 동시에 알려드리기 위해서 고안된 수업입니다. 수업대상 이 수업은 많은 웹페이지를 생산하면서 발생
opentutorials.org