Skip to content

Commit 8678be9

Browse files
committed
initial commit
0 parents  commit 8678be9

21 files changed

+975
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nbproject

Autoload.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* Check LICENSE file for license terms
5+
* (c) 2019. Samuel Onyijne, <[email protected]>
6+
*/
7+
8+
9+
/**
10+
* Description of Autoload
11+
*
12+
* @author samuel
13+
*/
14+
class Autoload
15+
{
16+
public static function register()
17+
{
18+
spl_autoload_register(function($class) {
19+
$file = dirname(__DIR__).DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
20+
if (file_exists($file)) {
21+
require $file;
22+
return true;
23+
}
24+
return false;
25+
});
26+
}
27+
}
28+
Autoload::register();

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# mini-mvc

config/_db.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
/*
4+
* Check LICENSE file for license terms
5+
* (c) 2019. Samuel Onyijne, <[email protected]> *
6+
*/
7+
return [
8+
"dsn" => 'mysql:host=localhost;dbname=mini',
9+
"username" => "root",
10+
"password" => "pass"
11+
];

config/main.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* Check LICENSE file for license terms
5+
* (c) 2019. Samuel Onyijne, <[email protected]> *
6+
*/
7+
$config = [
8+
"db" => require __DIR__.DIRECTORY_SEPARATOR.'_db.php',
9+
"url" => [
10+
"pretty" => true //for simplicity sake, this only implies to controller (r) and action (a)
11+
],
12+
'defaultRoute' => 'app/home',
13+
'error' => 'app/error'
14+
];
15+
16+
return $config;

controllers/AppController.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* Check LICENSE file for license terms
5+
* (c) 2019. Samuel Onyijne, <[email protected]> *
6+
*/
7+
8+
namespace mini\controllers;
9+
10+
use mini\core\Sam;
11+
use mini\core\Response;
12+
13+
/**
14+
* Description of AppController
15+
*
16+
* @author samuel
17+
*/
18+
class AppController extends \mini\core\Controller
19+
{
20+
21+
public function actionHome()
22+
{
23+
return $this->render('home',[
24+
'task' => Sam::$ony->getRequest()->get('task'),
25+
'id' => Sam::$ony->getRequest()->get('id'),
26+
]);
27+
}
28+
29+
public function actionError()
30+
{
31+
return $this->render('error');
32+
}
33+
}

controllers/TaskController.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* Check LICENSE file for license terms
5+
* (c) 2019. Samuel Onyijne, <[email protected]> *
6+
*/
7+
8+
namespace mini\controllers;
9+
10+
use mini\core\Sam;
11+
use mini\core\Response;
12+
use mini\models\Task;
13+
14+
/**
15+
* Description of TaskController
16+
*
17+
* @author samuel
18+
*/
19+
class TaskController extends \mini\core\Controller
20+
{
21+
22+
public function __construct() {
23+
Sam::$ony->getResponse()->format = Response::FORMAT_JSON;
24+
parent::__construct();
25+
}
26+
27+
public function actionIndex()
28+
{
29+
return (new Task())->_data;
30+
}
31+
32+
public function actionView($id)
33+
{
34+
return (new Task())->demo($id);
35+
}
36+
37+
public function actionCreate()
38+
{
39+
if (!Sam::$ony->getRequest()->isPost) {
40+
throw new Exception('This method only accept post requests', 400);
41+
}
42+
return Task::addNew();
43+
}
44+
45+
public function actionDelete($id)
46+
{
47+
if (!Sam::$ony->getRequest()->isPost) {
48+
throw new Exception('This method only accept post requests', 400);
49+
}
50+
$model = (new User())->demo($id);
51+
if (!$model) {
52+
return [
53+
'status' => 'error',
54+
'message' => $id. ' not on file'
55+
];
56+
}
57+
if (!$model->delete()) {
58+
return [
59+
'status' => 'error',
60+
'message' => $id. ' could not be deleted.'
61+
];
62+
}
63+
return [
64+
'status' => 'success',
65+
'message' => $id.' was deleted successfully'
66+
];
67+
}
68+
}

controllers/UserController.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* Check LICENSE file for license terms
5+
* (c) 2019. Samuel Onyijne, <[email protected]> *
6+
*/
7+
8+
namespace mini\controllers;
9+
10+
use mini\core\Sam;
11+
use mini\core\Response;
12+
use mini\models\User;
13+
14+
/**
15+
* Description of UserController
16+
*
17+
* @author samuel
18+
*/
19+
class UserController extends \mini\core\Controller
20+
{
21+
22+
public function __construct() {
23+
Sam::$ony->getResponse()->format = Response::FORMAT_JSON;
24+
parent::__construct();
25+
}
26+
27+
public function actionIndex()
28+
{
29+
return (new User())->_data;
30+
}
31+
32+
public function actionView($id)
33+
{
34+
return (new User())->demo($id);
35+
}
36+
37+
public function actionCreate()
38+
{
39+
if (!Sam::$ony->getRequest()->isPost) {
40+
throw new Exception('This method only accept post requests', 400);
41+
}
42+
return User::addNew();
43+
}
44+
45+
public function actionDelete($id)
46+
{
47+
if (!Sam::$ony->getRequest()->isPost) {
48+
throw new Exception('This method only accept post requests', 400);
49+
}
50+
$model = (new User())->demo($id);
51+
if (!$model) {
52+
return [
53+
'status' => 'error',
54+
'message' => $id. ' not on file'
55+
];
56+
}
57+
if (!$model->delete()) {
58+
return [
59+
'status' => 'error',
60+
'message' => $id. ' could not be deleted.'
61+
];
62+
}
63+
return [
64+
'status' => 'success',
65+
'message' => $id.' was deleted successfully'
66+
];
67+
}
68+
}

core/Controller.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* Check LICENSE file for license terms
5+
* (c) 2019. Samuel Onyijne, <[email protected]> *
6+
*/
7+
8+
namespace mini\core;
9+
use mini\core\Sam;
10+
/**
11+
* The base Controller
12+
*
13+
* @author samuel
14+
*/
15+
abstract class Controller
16+
{
17+
protected $layout = 'main';
18+
19+
public $defaultAction = 'index';
20+
21+
public $data = [];
22+
23+
public function __construct() {
24+
25+
}
26+
27+
public function set($data)
28+
{
29+
$this->data = array_merge($data, $this->data);
30+
}
31+
32+
protected function render($view, $data = [])
33+
{
34+
$c = strtolower(str_ireplace('Controller', '', get_called_class()));
35+
extract(array_merge($this->data, $data));
36+
ob_start();
37+
require VIEWS_PATH.DIRECTORY_SEPARATOR. trim($c, 'mini\\s\\').DIRECTORY_SEPARATOR.$view.'.php';
38+
$content = ob_get_clean();
39+
if ($this->layout) {
40+
require VIEWS_PATH.DIRECTORY_SEPARATOR.'layouts'.DIRECTORY_SEPARATOR.$this->layout.'.php';
41+
} else {
42+
echo $content;
43+
}
44+
}
45+
46+
public function hasMethod($name)
47+
{
48+
if (!method_exists($this, $name)) {
49+
return FALSE;
50+
} else {
51+
return TRUE;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)