From c8de66d130b1bbaa1ee5816aa0b36a00213fa96d Mon Sep 17 00:00:00 2001 From: BeardyMike <[email protected]> Date: Wed, 31 Jan 2024 16:52:26 +0000 Subject: [PATCH 1/2] Implement the new view() function in Controllers Updating the Controllers and CoreFunction.php to use the new view() function, as per the approved pull request (#61) --- application/Controller/ErrorController.php | 6 ++-- application/Controller/HomeController.php | 18 +++++----- application/Controller/SongsController.php | 39 ++++++++++++---------- application/Core/CoreFunctions.php | 15 +++++++++ 4 files changed, 48 insertions(+), 30 deletions(-) create mode 100644 application/Core/CoreFunctions.php diff --git a/application/Controller/ErrorController.php b/application/Controller/ErrorController.php index f87a7d5..3845a7d 100644 --- a/application/Controller/ErrorController.php +++ b/application/Controller/ErrorController.php @@ -20,8 +20,8 @@ class ErrorController public function index() { // load views - require APP . 'view/_templates/header.php'; - require APP . 'view/error/index.php'; - require APP . 'view/_templates/footer.php'; + view('_templates/header.php'); + view('error/index.php'); + view('_templates/footer.php'); } } diff --git a/application/Controller/HomeController.php b/application/Controller/HomeController.php index 0f3b0af..d014ab2 100644 --- a/application/Controller/HomeController.php +++ b/application/Controller/HomeController.php @@ -20,9 +20,9 @@ class HomeController public function index() { // load views - require APP . 'view/_templates/header.php'; - require APP . 'view/home/index.php'; - require APP . 'view/_templates/footer.php'; + view('_templates/header.php'); + view('home/index.php'); + view('_templates/footer.php'); } /** @@ -33,9 +33,9 @@ public function index() public function exampleOne() { // load views - require APP . 'view/_templates/header.php'; - require APP . 'view/home/example_one.php'; - require APP . 'view/_templates/footer.php'; + view('_templates/header.php'); + view('home/example_one.php'); + view('_templates/footer.php'); } /** @@ -46,8 +46,8 @@ public function exampleOne() public function exampleTwo() { // load views - require APP . 'view/_templates/header.php'; - require APP . 'view/home/example_two.php'; - require APP . 'view/_templates/footer.php'; + view('_templates/header.php'); + view('home/example_two.php'); + view('_templates/footer.php'); } } diff --git a/application/Controller/SongsController.php b/application/Controller/SongsController.php index f572577..1e10e3d 100644 --- a/application/Controller/SongsController.php +++ b/application/Controller/SongsController.php @@ -30,10 +30,10 @@ public function index() $songs = $Song->getAllSongs(); $amount_of_songs = $Song->getAmountOfSongs(); - // load views. within the views we can echo out $songs and $amount_of_songs easily - require APP . 'view/_templates/header.php'; - require APP . 'view/songs/index.php'; - require APP . 'view/_templates/footer.php'; + // load views. within the views we can echo out $songs and $amount_of_songs easily + view('_templates/header.php'); + view('songs/index.php', ["songs" => $songs]); + view('_templates/footer.php'); } /** @@ -51,11 +51,11 @@ public function addSong() // Instance new Model (Song) $Song = new Song(); // do addSong() in model/model.php - $Song->addSong($_POST["artist"], $_POST["track"], $_POST["link"]); + $Song->addSong($_POST["artist"], $_POST["track"], $_POST["link"]); } // where to go after song has been added - header('location: ' . URL . 'songs/index'); + redirect('songs/index'); } /** @@ -78,10 +78,10 @@ public function deleteSong($song_id) } // where to go after song has been deleted - header('location: ' . URL . 'songs/index'); + redirect('songs/index'); } - /** + /** * ACTION: editSong * This method handles what happens when you move to http://yourproject/songs/editsong * @param int $song_id Id of the to-edit song @@ -95,16 +95,19 @@ public function editSong($song_id) // do getSong() in model/model.php $song = $Song->getSong($song_id); - // in a real application we would also check if this db entry exists and therefore show the result or - // redirect the user to an error page or similar - - // load views. within the views we can echo out $song easily - require APP . 'view/_templates/header.php'; - require APP . 'view/songs/edit.php'; - require APP . 'view/_templates/footer.php'; + // If the song wasn't found, then it would have returned false, and we need to display the error page + if ($song === false) { + $page = new \Mini\Controller\ErrorController(); + $page->index(); + } else { + // load views. within the views we can echo out $song easily + view('_templates/header.php'); + view('songs/edit.php', ["song" => $song]); + view('_templates/footer.php'); + } } else { // redirect user to songs index page (as we don't have a song_id) - header('location: ' . URL . 'songs/index'); + redirect('songs/index'); } } @@ -123,11 +126,11 @@ public function updateSong() // Instance new Model (Song) $Song = new Song(); // do updateSong() from model/model.php - $Song->updateSong($_POST["artist"], $_POST["track"], $_POST["link"], $_POST['song_id']); + $Song->updateSong($_POST["artist"], $_POST["track"], $_POST["link"], $_POST['song_id']); } // where to go after song has been added - header('location: ' . URL . 'songs/index'); + redirect('songs/index'); } /** diff --git a/application/Core/CoreFunctions.php b/application/Core/CoreFunctions.php new file mode 100644 index 0000000..3bad3bc --- /dev/null +++ b/application/Core/CoreFunctions.php @@ -0,0 +1,15 @@ + $v) { + $$k = $v; + } + require APP . "view/{$path}"; +} + +function redirect($path) { + header('location: ' . URL . $path); +} From 05b199dedc21c0aaf1a659c5f382fda4d8c4c990 Mon Sep 17 00:00:00 2001 From: BeardyMike <[email protected]> Date: Thu, 1 Feb 2024 09:16:23 +0000 Subject: [PATCH 2/2] Update Core/Aplication.php for view() function added a line to pull the core/CoreFunctions.php into the core/Application.php. This allows the view() function to work. --- application/Core/Application.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/application/Core/Application.php b/application/Core/Application.php index e6cb686..4785700 100644 --- a/application/Core/Application.php +++ b/application/Core/Application.php @@ -2,6 +2,8 @@ /** For more info about namespaces plase @see http://php.net/manual/en/language.namespaces.importing.php */ namespace Mini\Core; +require APP . 'core/CoreFunctions.php'; + class Application { /** @var null The controller */