diff options
| -rw-r--r-- | public/delete/index.php | 5 | ||||
| -rw-r--r-- | public/index.php | 10 | ||||
| -rw-r--r-- | public/store/index.php | 7 | ||||
| -rw-r--r-- | public/update/index.php | 5 | ||||
| -rw-r--r-- | src/DTS/Session.php | 41 | ||||
| -rw-r--r-- | src/templates/index.php | 3 |
6 files changed, 69 insertions, 2 deletions
diff --git a/public/delete/index.php b/public/delete/index.php index e07129a..9864c9a 100644 --- a/public/delete/index.php +++ b/public/delete/index.php @@ -3,6 +3,7 @@ declare(strict_types=1); use DTS\BookmarkRepository; +use DTS\Session; use function DTS\Functions\respondAndExit; use function DTS\Functions\redirectAndExit; @@ -10,6 +11,8 @@ require_once(__DIR__.'/../../autoload.php'); $config = require_once(__DIR__.'/../../config.php'); +$session = Session::getInstance(); + if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') { respondAndExit(405, 'Method Not Allowed'); } @@ -28,4 +31,6 @@ if (!$bookmarks->delete($bookmark)) { respondAndExit(500, 'Internal Server Error'); } +$session->set('message', 'Bookmark Deleted'); + redirectAndExit('/'); diff --git a/public/index.php b/public/index.php index 7453506..025c9ec 100644 --- a/public/index.php +++ b/public/index.php @@ -3,6 +3,7 @@ declare(strict_types=1); use DTS\BookmarkRepository; +use DTS\Session; use DTS\Template; use function DTS\Functions\respondAndExit; @@ -10,6 +11,8 @@ require_once(__DIR__.'/../autoload.php'); $config = require_once(__DIR__.'/../config.php'); +$session = Session::getInstance(); + $bookmarks = new BookmarkRepository($config['path_to_repository']); $template = new Template($config['path_to_templates']); @@ -24,6 +27,11 @@ if ($tag !== null) { $bookmarks->sort($sort === 'asc'); -$html = $template->render('index', compact('bookmarks')); +$message = $session->get('message'); + +$html = $template->render('index', compact( + 'bookmarks', + 'message' +)); respondAndExit(200, 'OK', $html); diff --git a/public/store/index.php b/public/store/index.php index 2a84c59..45765d5 100644 --- a/public/store/index.php +++ b/public/store/index.php @@ -4,13 +4,16 @@ declare(strict_types=1); use DTS\Bookmark; use DTS\BookmarkRepository; -use function DTS\Functions\respondAndExit; +use DTS\Session; use function DTS\Functions\redirectAndExit; +use function DTS\Functions\respondAndExit; require_once(__DIR__.'/../../autoload.php'); $config = require_once(__DIR__.'/../../config.php'); +$session = Session::getInstance(); + if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') { respondAndExit(405, 'Method Not Allowed'); } @@ -30,4 +33,6 @@ if (!$bookmarks->add($bookmark)) { respondAndExit(500, 'Internal Server Error'); } +$session->set('message', 'Bookmark Added'); + redirectAndExit('/'); diff --git a/public/update/index.php b/public/update/index.php index fa6b482..50d0dcf 100644 --- a/public/update/index.php +++ b/public/update/index.php @@ -3,6 +3,7 @@ declare(strict_types=1); use DTS\BookmarkRepository; +use DTS\Session; use function DTS\Functions\respondAndExit; use function DTS\Functions\redirectAndExit; @@ -10,6 +11,8 @@ require_once(__DIR__.'/../../autoload.php'); $config = require_once(__DIR__.'/../../config.php'); +$session = Session::getInstance(); + if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') { respondAndExit(405, 'Method Not Allowed'); } @@ -32,4 +35,6 @@ if (!$bookmarks->update($bookmark)) { respondAndExit(500, 'Internal Server Error'); } +$session->set('message', 'Bookmark Updated'); + redirectAndExit('/'); diff --git a/src/DTS/Session.php b/src/DTS/Session.php new file mode 100644 index 0000000..956f4e5 --- /dev/null +++ b/src/DTS/Session.php @@ -0,0 +1,41 @@ +<?php + +declare(strict_types=1); + +namespace DTS; + +class Session +{ + private static ?self $instance = null; + + private array $session; + + public static function getInstance() + { + if (!self::$instance) { + self::$instance = new self(); + } + + return self::$instance; + } + + private function __construct() + { + session_start(); + + foreach ($_SESSION as $key => $value) { + $this->session[$key] = $value; + unset($_SESSION[$key]); + } + } + + public function set(string $key, string $value): void + { + $this->session[$key] = $_SESSION[$key] = $value; + } + + public function get(string $key): ?string + { + return $this->session[$key] ?? null; + } +} diff --git a/src/templates/index.php b/src/templates/index.php index ed84fd4..148a173 100644 --- a/src/templates/index.php +++ b/src/templates/index.php @@ -6,6 +6,9 @@ <title>Bookmarks</title> </head> <body> + <?php if ($message) { ?> + <p><?php echo $message; ?></p> + <?php } ?> <a href="/create">Add</a> <ul> <?php foreach ($bookmarks as $bookmark) { ?> |
