From 50781c4c49032b249fc23bf8999efbe508b43274 Mon Sep 17 00:00:00 2001
From: foadmk
Date: Tue, 2 Mar 2021 04:07:42 -0300
Subject: [PATCH] adding view and redirect function to clean up controllers
---
.gitignore | 1 +
application/Controller/ErrorController.php | 6 ++---
application/Controller/HomeController.php | 19 ++++++++-------
application/Controller/SongsController.php | 28 +++++++++++-----------
application/Core/Application.php | 4 +++-
application/Core/CoreFunctions.php | 16 +++++++++++++
6 files changed, 47 insertions(+), 27 deletions(-)
create mode 100644 .gitignore
create mode 100644 application/Core/CoreFunctions.php
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..208a599
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+vendor/*
\ No newline at end of file
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..994e7a7 100644
--- a/application/Controller/HomeController.php
+++ b/application/Controller/HomeController.php
@@ -20,9 +20,10 @@ 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 +34,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 +47,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 b791c06..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
@@ -101,13 +101,13 @@ public function editSong($song_id)
$page->index();
} else {
// 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';
+ 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');
}
}
@@ -126,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/Application.php b/application/Core/Application.php
index 083674c..ed42bff 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 */
@@ -39,7 +41,7 @@ public function __construct()
// check for method: does such a method exist in the controller ?
if (method_exists($this->url_controller, $this->url_action) &&
is_callable(array($this->url_controller, $this->url_action))) {
-
+
if (!empty($this->url_params)) {
// Call the method and pass arguments to it
call_user_func_array(array($this->url_controller, $this->url_action), $this->url_params);
diff --git a/application/Core/CoreFunctions.php b/application/Core/CoreFunctions.php
new file mode 100644
index 0000000..4caccf1
--- /dev/null
+++ b/application/Core/CoreFunctions.php
@@ -0,0 +1,16 @@
+ $v) {
+ $$k = $v;
+ }
+ require APP . "view/{$path}";
+}
+
+
+function redirect($path) {
+ header('location: ' . URL . $path);
+}
\ No newline at end of file