Skip to content

Commit

Permalink
Merge pull request #8 from pr-YudaiSuzuki/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
szkyudi authored May 25, 2020
2 parents c480cfb + 7ffde0c commit 373650a
Show file tree
Hide file tree
Showing 57 changed files with 1,726 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.env
docker/mysql/data
docker/phpmyadmin/sessions/**
/html/uploads/**
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,41 @@
# PHP/MySQLで記事投稿サービスを作る
# PHP/MySQLで記事投稿サービスを作る
## リンク
- トップページ: [/](http://18.180.139.159) or [/index.php](http://18.180.139.159)
- ユーザー記事一覧ページ: [/user.php?id=<screen_name>](http://18.180.139.159/user.php?id=suzuki)
- タグ記事一覧ページ: [/tag.php?name=<tag_name>](http://18.180.139.159/tag.php?name=IT)
- タグ一覧ページ: [/tags.php](http://18.180.139.159/tags.php)
- 新規登録ページ: [/signup.php](http://18.180.139.159/signup.php)
- ログインページ: [/login.php](http://18.180.139.159/login.php)
- 記事作成ページ: [/create.php](http://18.180.139.159/create.php)
- 記事ページ: [/post.php?id=<post_slug>](http://18.180.139.159/post.php?id=4e93dc98-9e3c-11ea-85c7-0242ac140002)
- 記事編集ページ: [/edit.php?id=<post_slug>](http://18.180.139.159/edit.php?id=4e93dc98-9e3c-11ea-85c7-0242ac140002)

## ディレクトリについて
### `/docker`
環境構築に関する環境変数や初期設定ファイルが含まれています。懸念点として `php.ini` が若干アプリケーションに依存しているので、別のディレクトリがあってもいいのかなと思いました。

### `/html`
`DOCUMENT_ROOT` となるディレクトリです。
- `config`: 初期設定用ファイル
- `uploads`: アップロードされた画像ファイルの保存場所
- `その他`:各コントローラーの処理を発火させる簡易的なルーター

### `/src`
実際どうなっているかは分かりませんが、自分ではMVCを意識して作ったつもりです。

#### `/src/app`
MVCの初期設定に関する処理をまとめたディレクトリです。思いついた機能を逐一追加していたので、若干整理されていない感じがあるかもしれません。セッションの有効期限は `0` に設定しているので、ブラウザを終了したらセッションが終了するはずです。

#### `/src/controllers`
ルーターからコントローラーの処理を発火させています。モデルから整形されたデータを取得したり、リクエストに応じてバリデーションを行っています。

#### `/src/models`
データベースのレコードをクラスに整形しています。データベースの操作は主に `Manager` に一任していて、`Model``Image` モデルのコンストラクタ以外は何も処理をしていないピュアなオブジェクトです。モデルのプロパティを読み取り専用にできたらもっと良いのかなと思っています。

#### `/src/views`
コントローラから値を受け取って画面に表示するテンプレートです。`base/header` ではセッションから現在のユーザーを取得しています。分岐を追加することでもっと柔軟な表示ができると思いますが、今回は最低限に留めています。

## データベースについて
`/docker/mysql/docker/create_tables.sql` にSQLが記載されています。
正規化やER図については[コチラ](https://docs.google.com/document/d/18gjLpb50B0Tw7mzjgUQmLjeG4KCfIDowam-oSVPscdE/edit#heading=h.pj2ha2haaolw)のGoogle Documentにまとめてあります。

45 changes: 45 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: '3'
services:
php:
container_name: php
build:
context: ./docker/php
dockerfile: Dockerfile
tty: true
volumes:
- ./docker/php/php.ini:/usr/local/etc/php.ini
- ./html:/var/www/html
- ./src:/src
env_file:
- ./docker/php/php.env
ports:
- 80:80
networks:
- kenshu-lamp

mysql:
container_name: mysql
image: mysql:8.0
env_file:
- ./docker/mysql/mysql.env
volumes:
- ./docker/mysql/data:/var/lib/mysql
- ./docker/mysql/mysql_conf:/etc/mysql/conf.d
- ./docker/mysql/docker:/docker
networks:
- kenshu-lamp

phpmyadmin:
container_name: pma
image: phpmyadmin/phpmyadmin:5.0
env_file:
- ./docker/phpmyadmin/pma.env
volumes:
- ./docker/phpmyadmin/sessions:/sessions
networks:
- kenshu-lamp
ports:
- 8080:80

networks:
kenshu-lamp:
70 changes: 70 additions & 0 deletions docker/mysql/docker/create_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
DROP DATABASE IF EXISTS kenshu;
CREATE DATABASE kenshu;
USE kenshu;

CREATE TABLE users (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
screen_name VARCHAR(20) NOT NULL UNIQUE,
name VARCHAR(50) NOT NULL,
password_hash VARCHAR(255) NOT NULL,

INDEX (screen_name)
);

CREATE TABLE posts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
slug VARCHAR(255) NOT NULL DEFAULT (UUID()) UNIQUE,
user_id INT UNSIGNED NOT NULL,
title VARCHAR(100) NOT NULL DEFAULT '',
body TEXT NOT NULL,
published_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
is_open BOOLEAN NOT NULL DEFAULT FALSE,

INDEX (slug, title, published_at),

CONSTRAINT fk_user_id_from_posts
FOREIGN KEY (user_id)
REFERENCES users (id)
ON DELETE CASCADE
);

CREATE TABLE images (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
url TEXT NOT NULL
);

CREATE TABLE post_images (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
post_id INT UNSIGNED,
image_id INT UNSIGNED,
is_thumbnail BOOLEAN NOT NULL DEFAULT FALSE,

CONSTRAINT fk_post_id_from_post_images
FOREIGN KEY (post_id)
REFERENCES posts (id)
ON DELETE CASCADE
);

CREATE TABLE tags (
id int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE,

INDEX (name)
);

CREATE TABLE post_tags (
post_id INT UNSIGNED,
tag_id int UNSIGNED,

PRIMARY KEY (post_id, tag_id),

CONSTRAINT fk_post_id_from_post_tags
FOREIGN KEY (post_id)
REFERENCES posts (id)
ON DELETE CASCADE,

CONSTRAINT fk_tag_id_from_post_tags
FOREIGN KEY (tag_id)
REFERENCES tags (id)
ON DELETE CASCADE
);
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions docker/mysql/docker/insert_dummy.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
insert into users (screen_name, name, password_hash) values
('suzuki', 'SUZUKI', '$2y$10$VibulVO8mbKF3p92lFHgN.wV4jO.410G0H5wg/8Q56mMVZHjKhrHS'),
('yudai', 'Yudai', '$2y$10$QAbF5/L5vjtrtqkYlINsvOJ14.8HuTJLTtuKAETPfYNnp45zeJlne');

insert into posts (user_id, title, published_at, is_open, body) values
(1, 'hello world', '2020-05-01 19:00:00', TRUE,'Eu minim minim nulla do ad proident exercitation duis mollit nulla magna velit. Velit eu excepteur labore sunt cillum minim labore aliquip laboris excepteur eiusmod sint laboris est. Et velit ex voluptate sint officia laboris nulla deserunt ipsum. Ea consequat ex aliquip ullamco officia. Incididunt nostrud amet deserunt elit ut exercitation velit id quis id ea aliquip adipisicing ullamco. Id proident mollit incididunt sit ea aliquip nisi aliqua sint laboris aute labore sunt consequat. Et eu aliqua labore esse adipisicing consectetur cupidatat fugiat.'),
(1, 'good morning', '2020-05-03 17:00:00', FALSE, 'Elit ipsum esse tempor eu dolore dolore tempor sunt dolore commodo. Officia qui sit ipsum sint ipsum voluptate velit anim eu anim voluptate. Mollit duis adipisicing culpa et aliqua et qui anim cillum officia deserunt id tempor duis. Proident ipsum velit incididunt cupidatat labore adipisicing enim id consectetur elit nulla nulla amet.'),
(2, 'good evening', '2020-05-07 13:00:00', TRUE, 'Aliquip aute officia cupidatat enim id tempor id Lorem velit eu reprehenderit ad. Ullamco elit enim non deserunt id ipsum nisi ut est. Labore ad occaecat nostrud nisi eiusmod quis proident duis deserunt ad ex. Nulla et id sit eu et voluptate culpa ut. Commodo ut amet qui non eu. Ipsum enim nulla cupidatat eiusmod proident labore sunt. Ex sit eu occaecat nulla reprehenderit.');

insert into images (url) values
('4437d0b0d88faca23e28c0db86bcae72.jpg'),
('34c9e802a15aad46adbc310b705a96b3.jpg'),
('9427c152b2cfec5ea8d6c26871ebb1e5.jpg'),
('b532d664b56db543b3eba68a4947dd34.jpg'),
('9879a11b73d0d653a39299acf754158e.jpg'),
('5f5e712d37055f34689684277bf680a0.jpg');

insert into post_images (post_id, image_id, is_thumbnail) values
(1, 1, true),
(1, 2, false),
(1, 3, false),
(2, 4, true),
(2, 5, false),
(3, 6, true);

insert into tags (name) values
('IT'),
('AI'),
('IoT'),
('Game');

insert into post_tags (post_id, tag_id) values
(1, 1),
(1, 2),
(1, 3),
(2, 1),
(2, 4);
2 changes: 2 additions & 0 deletions docker/mysql/mysql_conf/custom.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[mysqld]
default_authentication_plugin=mysql_native_password
3 changes: 3 additions & 0 deletions docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM php:7.3-apache
RUN apt-get update && \
docker-php-ext-install pdo_mysql mysqli mbstring exif
18 changes: 18 additions & 0 deletions docker/php/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[Data]
date.timezone = "Asia/Tokyo"

[mbstring]
mbstring.internal_encoding = "UTF-8"
mbstring.language = "Japanese"

[session]
session.name = "SESSID"
session.maxlifetime = 0
session.cookie_lifetime = 0
session.use_strict_mode = on
session.cookie_httponly = on
session.cookie_samesite = "Lax"

[extension]
extension=php_mbstring.dll
extension=php_exif.dll
4 changes: 4 additions & 0 deletions html/config/environment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// ソースまでの絶対パス
define('APP_DIR', "/src");
12 changes: 12 additions & 0 deletions html/create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

require_once __DIR__."/config/environment.php";
require_once APP_DIR."/app/session.php";
require_once APP_DIR."/controllers/createController.php";


if ($_SERVER['REQUEST_METHOD'] == "GET") {
get();
} elseif ($_SERVER['REQUEST_METHOD'] == "POST") {
post($_POST);
}
11 changes: 11 additions & 0 deletions html/delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require_once __DIR__."/config/environment.php";
require_once APP_DIR."/app/session.php";
require_once APP_DIR."/controllers/deleteController.php";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
post($_POST['slug']);
} else {
include APP_DIR."/views/404.php";
}
11 changes: 11 additions & 0 deletions html/edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require_once __DIR__."/config/environment.php";
require_once APP_DIR."/app/session.php";
require_once APP_DIR."/controllers/editController.php";

if ($_SERVER['REQUEST_METHOD'] == "GET") {
get($_GET['id']);
} elseif ($_SERVER['REQUEST_METHOD'] == "POST") {
post($_POST);
}
11 changes: 11 additions & 0 deletions html/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require_once __DIR__."/config/environment.php";
require_once APP_DIR."/app/session.php";
require_once APP_DIR."/controllers/indexController.php";

if ($_SERVER["REQUEST_METHOD"] === "GET") {
get();
} else {
include APP_DIR."/views/404.php";
}
12 changes: 12 additions & 0 deletions html/login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

require_once __DIR__."/config/environment.php";
require_once APP_DIR."/app/session.php";
require_once APP_DIR."/controllers/loginController.php";


if ($_SERVER['REQUEST_METHOD'] == "GET") {
get();
} elseif ($_SERVER['REQUEST_METHOD'] == "POST") {
post($_POST['screen_name'], $_POST['password']);
}
12 changes: 12 additions & 0 deletions html/logout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

require_once __DIR__."/config/environment.php";
require_once APP_DIR."/app/session.php";
require_once APP_DIR."/controllers/logoutController.php";


if ($_SERVER['REQUEST_METHOD'] == "GET") {
get();
} else {
include APP_DIR."/views/404.php";
}
11 changes: 11 additions & 0 deletions html/post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require_once __DIR__."/config/environment.php";
require_once APP_DIR."/app/session.php";
require_once APP_DIR."/controllers/postController.php";

if ($_SERVER["REQUEST_METHOD"] === "GET") {
get($_GET['id']);
} else {
include APP_DIR."/views/404.php";
}
12 changes: 12 additions & 0 deletions html/signup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

require_once __DIR__."/config/environment.php";
require_once APP_DIR."/app/session.php";
require_once APP_DIR."/controllers/signupController.php";


if ($_SERVER['REQUEST_METHOD'] == "GET") {
get();
} elseif ($_SERVER['REQUEST_METHOD'] == "POST") {
post($_POST['screen_name'], $_POST['name'], $_POST['password']);
}
11 changes: 11 additions & 0 deletions html/tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require_once __DIR__."/config/environment.php";
require_once APP_DIR."/app/session.php";
require_once APP_DIR."/controllers/tagController.php";

if ($_SERVER["REQUEST_METHOD"] === "GET") {
get($_GET['name']);
} else {
include APP_DIR."/views/404.php";
}
11 changes: 11 additions & 0 deletions html/tags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require_once __DIR__."/config/environment.php";
require_once APP_DIR."/app/session.php";
require_once APP_DIR."/controllers/tagsController.php";

if ($_SERVER["REQUEST_METHOD"] === "GET") {
get();
} else {
include APP_DIR."/views/404.php";
}
12 changes: 12 additions & 0 deletions html/user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

require_once __DIR__."/config/environment.php";
require_once APP_DIR."/app/session.php";
require_once APP_DIR."/controllers/userController.php";


if ($_SERVER["REQUEST_METHOD"] === "GET") {
get($_GET['id']);
} else {
include APP_DIR."/views/404.php";
}
Loading

0 comments on commit 373650a

Please sign in to comment.