Skip to content

Commit

Permalink
Merge pull request #61 from foadmk/adding_core_functions
Browse files Browse the repository at this point in the history
adding view and redirect function to clean up controllers
  • Loading branch information
panique authored Aug 8, 2021
2 parents b296526 + 50781c4 commit bf66697
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/*
6 changes: 3 additions & 3 deletions application/Controller/ErrorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
19 changes: 10 additions & 9 deletions application/Controller/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

}

/**
Expand All @@ -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');
}

/**
Expand All @@ -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');
}
}
28 changes: 14 additions & 14 deletions application/Controller/SongsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand All @@ -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');
}

/**
Expand All @@ -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
Expand All @@ -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');
}
}

Expand All @@ -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');
}

/**
Expand Down
4 changes: 3 additions & 1 deletion application/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions application/Core/CoreFunctions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Mini\Controller;

function view($path, $data = [])
{
foreach ($data as $k => $v) {
$$k = $v;
}
require APP . "view/{$path}";
}


function redirect($path) {
header('location: ' . URL . $path);
}

0 comments on commit bf66697

Please sign in to comment.