Skip to content

Commit

Permalink
initial commit (basic MVC barebone)
Browse files Browse the repository at this point in the history
  • Loading branch information
panique committed Nov 9, 2013
1 parent ccc1691 commit 0d14f03
Show file tree
Hide file tree
Showing 20 changed files with 545 additions and 20 deletions.
13 changes: 13 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Activates URL rewriting (like myproject.com/controller/action/1/2/3)
RewriteEngine On

# When using the script within a sub-folder, put this path here, like /mysubfolder/
# If your app is in the root of your web folder, then leave it commented out
RewriteBase /php-mvc/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

#
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
23 changes: 23 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Structure

@TODO: dokumentation
@TODO: infografik




As manually as possible, as simple as possible. No automation when not needed.


# TODO:

Always open only one database connection
Loading two models

# Code styles

PSR 1/2
Modern PHP does not use closing tags

20 changes: 0 additions & 20 deletions LICENSE

This file was deleted.

27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,30 @@
A simple and easy to understand MVC skeleton application.
Perfect for quickly building real and clean applications.
Perfect to learn how MVC works.

# License

This project is licensed under the MIT License.
This means you can use and modify it for free in private or commercial projects.

The MIT License (MIT)

Copyright (c) 2013 Panique

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

31 changes: 31 additions & 0 deletions application/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* CONFIGURATION
*
* For more info about constants please @see http://php.net/manual/en/function.define.php
* If you want to know why we use "define" instead of "const" @see http://stackoverflow.com/q/2447791/1114320
*/

/**
* Configuration for: Error reporting
* Useful to show every little problem during development, but only show hard errors in production
*/
error_reporting(E_ALL);
ini_set("display_errors", 1);

/**
* Configuration for: Project URL
* Put your URL here, for local development 127.0.0.1 or localhost (plus subfolder) is fine
*/
define('URL', 'http://127.0.0.1/php-mvc/');

/**
* Configuration for: Database
* This is the place where you define your database credentials, type etc.
*/
define('DB_TYPE', 'mysql');
define('DB_HOST', '127.0.0.1');
define('DB_NAME', 'php-mvc');
define('DB_USER', 'root');
define('DB_PASS', 'mysql');
42 changes: 42 additions & 0 deletions application/controller/home.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Class Home
*
* Please note:
* Don't use the same name for class and method, as this might trigger an (unintended) __construct of the class.
* This is really weird behaviour, but documented here: http://php.net/manual/en/language.oop5.decon.php
*
*/
class Home
{
public function index()
{
// debug message to show where you are
echo 'Message from Controller: You are in the controller home, using the method index()';
// load view
require 'application/views/_templates/header.php';
require 'application/views/home/index.php';
require 'application/views/_templates/footer.php';
}

public function exampleOne()
{
// debug message to show where you are
echo 'Message from Controller: You are in the controller home, using the method exampleOne()';
// load view
require 'application/views/_templates/header.php';
require 'application/views/home/example_one.php';
require 'application/views/_templates/footer.php';
}

public function exampleTwo()
{
// debug message to show where you are
echo 'Message from Controller: You are in the controller home, using the method exampleTwo()';
// load view
require 'application/views/_templates/header.php';
require 'application/views/home/example_two.php';
require 'application/views/_templates/footer.php';
}
}
70 changes: 70 additions & 0 deletions application/controller/songs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* Class Songs
* This is a demo class.
*
* Please note:
* Don't use the same name for class and method, as this might trigger an (unintended) __construct of the class.
* This is really weird behaviour, but documented here: http://php.net/manual/en/language.oop5.decon.php
*
*/
class Songs
{
/**
* PAGE:
*/
public function index()
{
// simple message to show where you are
echo 'Message from Controller: You are in the Controller: Songs, using the method index().';

// load model, perform an action on the model
require 'application/models/songs_model.php';
$songs_model = new Songs_Model();
$songs_model->getAllSongs();

// load header and view
require 'application/views/_templates/header.php';
require 'application/views/songs/index.php';
require 'application/views/_templates/footer.php';
}

/**
* ACTION:
*/
public function addSong()
{
// simple message to show where you are
echo 'Message from Controller: You are in the Controller: Songs, using the method addSong().';

if (isset($_POST["submit_add_song"])) {
// load model, perform an action on the model
require 'application/models/songs_model.php';
$songs_model = new Songs_Model();
$songs_model->addSong($_POST["artist"], $_POST["track"], $_POST["link"]);
}

// where to go after song has been added ?
header('location: ' . URL . 'songs/index');
}

/**
* ACTION:
*/
public function deleteSong($song_id)
{
// simple message to show where you are
echo 'Message from Controller: You are in the Controller: Songs, using the method deleteSong().';

if (isset($song_id)) {
// load model, perform an action on the model
require 'application/models/songs_model.php';
$songs_model = new Songs_Model();
$songs_model->deleteSong($song_id);
}

// where to go after song has been added ?
header('location: ' . URL . 'songs/index');
}
}
98 changes: 98 additions & 0 deletions application/libs/application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

class Application
{
/**
* @var null The controller
*/
private $url_controller = null;
/**
* @var null The method (of the above controller)
*/
private $url_action = null;
/**
* @var null Parameter one
*/
private $url_parameter_1 = null;
/**
* @var null Parameter two
*/
private $url_parameter_2 = null;
/**
* @var null Parameter three
*/
private $url_parameter_3 = null;

/**
* "Starts" the application:
* Analyzes the URL elements and calls the according controller/method or the fallback
*/
public function __construct()
{
// create array with URL parts in $url
$this->splitUrl();

// check for controller: does such an controller exist ?
if (file_exists('./application/controller/' . $this->url_controller . '.php')) {

// if so, then load this file and create this controller
// example: if controller would be "car", then this line would translate into: $this->car = new car();
require './application/controller/' . $this->url_controller . '.php';
$this->url_controller = new $this->url_controller();

// check for method: does such a method exist in the controller ?
if (method_exists($this->url_controller, $this->url_action)) {

// call the method and pass the parameters to it
if (isset($this->url_parameter_3)) {
$this->url_controller->{$this->url_action}($this->url_parameter_1, $this->url_parameter_2, $this->url_parameter_3);
} elseif (isset($this->url_parameter_2)) {
$this->url_controller->{$this->url_action}($this->url_parameter_1, $this->url_parameter_2);
} elseif (isset($this->url_parameter_1)) {
$this->url_controller->{$this->url_action}($this->url_parameter_1);
} else {
// if no parameters given, just call the method without parameters
$this->url_controller->{$this->url_action}();
}
} else {
// default/fallback: call the index() method of a selected controller
$this->url_controller->index();
}
} else {
// invalid URL, so simply show home/index
require './application/controller/home.php';
$home = new Home();
$home->index();
}
}

/**
* Gets and splits the URL
*/
private function splitUrl()
{
if (isset($_GET['url'])) {

// split URL
$url = rtrim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);

// Put URL parts into according properties
// By the way, the syntax here if just a short form of if/else, called "Ternary Operators"
// http://davidwalsh.name/php-shorthand-if-else-ternary-operators
$this->url_controller = (isset($url[0]) ? $url[0] : null);
$this->url_action = (isset($url[1]) ? $url[1] : null);
$this->url_parameter_1 = (isset($url[2]) ? $url[2] : null);
$this->url_parameter_2 = (isset($url[3]) ? $url[3] : null);
$this->url_parameter_3 = (isset($url[4]) ? $url[4] : null);
}

// DEBUG
// echo 'Controller: ' . $this->url_controller . '<br />';
// echo 'Action: ' . $this->url_action . '<br />';
// echo 'Parameter 1: ' . $this->url_parameter_1 . '<br />';
// echo 'Parameter 2: ' . $this->url_parameter_2 . '<br />';
// echo 'Parameter 3: ' . $this->url_parameter_3 . '<br />';
}
}
18 changes: 18 additions & 0 deletions application/libs/database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

class Database extends PDO {

public function __construct() {

// set the (optional) options of the PDO connection. in this case, we set the fetch mode to
// "objects", which means all results will be objects, like this: $result->user_name !
// For example, fetch mode FETCH_ASSOC would return results like this: $result["user_name] !
// @see http://www.php.net/manual/en/pdostatement.fetch.php
$options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING);

// generate a database connection, using the PDO connector
// @see http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
parent::__construct(DB_TYPE . ':host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS, $options);
}

}
Loading

0 comments on commit 0d14f03

Please sign in to comment.