Skip to content

Commit 8e8ba51

Browse files
committed
added database support
1 parent 0207754 commit 8e8ba51

26 files changed

+1296
-629
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
11
# mini-mvc
2+
3+
A mini MVC with for very small API/web projects that do not require much features.
4+
Can also be a good starting pint to learn how the big guys develop PHP frameworks.
5+
6+
Tested with MySQL and SQLite database.
7+
8+
---
9+
10+
TODO
11+
12+
-Add curl/streaming support
13+
-Add model validation
14+
-Add more Query options
15+
-Authentication
16+
-Two Factor Authentication
17+
-Basic view template

config/_db.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* Check LICENSE file for license terms
55
* (c) 2019. Samuel Onyijne, <[email protected]> *
66
*/
7+
$db_file = dirname(__DIR__).'/data/mini.db';
8+
79
return [
8-
"dsn" => 'mysql:host=localhost;dbname=mini',
9-
"username" => "root",
10-
"password" => "pass"
10+
"dsn" => 'sqlite:'.$db_file,
1111
];

controllers/AppController.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
*
1616
* @author samuel
1717
*/
18-
class AppController extends \mini\core\Controller
18+
class AppController extends \mini\core\web\Controller
1919
{
2020

21+
public $defaultAction = 'home';
22+
2123
public function actionHome()
2224
{
2325
return $this->render('home',[

controllers/TaskController.php

+2-86
Original file line numberDiff line numberDiff line change
@@ -7,99 +7,15 @@
77

88
namespace mini\controllers;
99

10-
use mini\core\Sam;
11-
use mini\core\Response;
12-
use mini\models\Task;
1310

1411
/**
1512
* Description of TaskController
1613
*
1714
* @author samuel
1815
*/
19-
class TaskController extends \mini\core\Controller
16+
class TaskController extends \mini\core\rest\Controller
2017
{
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-
return [
41-
'status' => 'error',
42-
'message' => 'method not supported'
43-
];
44-
}
45-
return Task::addNew();
46-
}
18+
public $modelClass = 'mini\models\Task';
4719

48-
public function actionUpdate()
49-
{
50-
if (!Sam::$ony->getRequest()->isPost) {
51-
return [
52-
'status' => 'error',
53-
'message' => 'method not supported'
54-
];
55-
}
56-
$id = Sam::$ony->getRequest()->post('id');
57-
$model = new Task();
58-
if (!$model->demo($id)) {
59-
return [
60-
'status' => 'error',
61-
'message' => $id. ' not on file'
62-
];
63-
}
64-
if (!$model->getModel($id)->update()) {
65-
return [
66-
'status' => 'error',
67-
'message' => $id. ' could not be updated.'
68-
];
69-
}
70-
return [
71-
'status' => 'success',
72-
'message' => $id.' was updated successfully',
73-
'current_data' => $model->_data
74-
];
75-
}
7620

77-
public function actionDelete()
78-
{
79-
if (!Sam::$ony->getRequest()->isPost) {
80-
return [
81-
'status' => 'error',
82-
'message' => 'method not supported'
83-
];
84-
}
85-
$id = Sam::$ony->getRequest()->post('id');
86-
$model = new User();
87-
if (!$model->demo($id)) {
88-
return [
89-
'status' => 'error',
90-
'message' => $id. ' not on file'
91-
];
92-
}
93-
if (!$model->delete()) {
94-
return [
95-
'status' => 'error',
96-
'message' => $id. ' could not be deleted.'
97-
];
98-
}
99-
return [
100-
'status' => 'success',
101-
'message' => $id.' was deleted successfully',
102-
'current_data' => $model->_data
103-
];
104-
}
10521
}

controllers/UserController.php

+2-89
Original file line numberDiff line numberDiff line change
@@ -7,100 +7,13 @@
77

88
namespace mini\controllers;
99

10-
use mini\core\Sam;
11-
use mini\core\Response;
12-
use mini\models\User;
13-
1410
/**
1511
* Description of UserController
1612
*
1713
* @author samuel
1814
*/
19-
class UserController extends \mini\core\Controller
15+
class UserController extends \mini\core\rest\Controller
2016
{
17+
public $modelClass = 'mini\models\User';
2118

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-
return [
41-
'status' => 'error',
42-
'message' => 'method not supported'
43-
];
44-
}
45-
return User::addNew();
46-
}
47-
48-
public function actionUpdate()
49-
{
50-
if (!Sam::$ony->getRequest()->isPost) {
51-
return [
52-
'status' => 'error',
53-
'message' => 'method not supported'
54-
];
55-
}
56-
$id = Sam::$ony->getRequest()->post('id');
57-
$model = new User();
58-
if (!$model->demo($id)) {
59-
return [
60-
'status' => 'error',
61-
'message' => $id. ' not on file'
62-
];
63-
}
64-
if (!$model->getModel($id)->update()) {
65-
return [
66-
'status' => 'error',
67-
'message' => $id. ' could not be updated.'
68-
];
69-
}
70-
return [
71-
'status' => 'success',
72-
'message' => $id.' was updated successfully',
73-
'current_data' => $model->_data
74-
];
75-
}
76-
77-
public function actionDelete()
78-
{
79-
if (!Sam::$ony->getRequest()->isPost) {
80-
return [
81-
'status' => 'error',
82-
'message' => 'method not supported'
83-
];
84-
}
85-
$id = Sam::$ony->getRequest()->post('id');
86-
$model = new User();
87-
if (!$model->demo($id)) {
88-
return [
89-
'status' => 'error',
90-
'message' => $id. ' not on file'
91-
];
92-
}
93-
if (!$model->delete()) {
94-
return [
95-
'status' => 'error',
96-
'message' => $id. ' could not be deleted.'
97-
];
98-
}
99-
return [
100-
'status' => 'success',
101-
'message' => $id.' was deleted successfully',
102-
'current_data' => $model->_data
103-
];
104-
}
105-
10619
}

core/Model.php

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
10+
/**
11+
* Description of Model
12+
*
13+
* @author samuel
14+
*/
15+
class Model implements \Iterator
16+
{
17+
private $position = 0;
18+
private $array = [];
19+
20+
/**
21+
* this method should be called first when overridden by a child class
22+
* like parent::__construct()
23+
*/
24+
public function __construct() {
25+
$this->position = 0;
26+
$array = [];
27+
$class = new \ReflectionClass(get_called_class());
28+
if ($class->hasMethod('tableName')) {
29+
$mode = $class->newInstanceWithoutConstructor();
30+
$array = $mode->setProperties();
31+
}
32+
foreach ($class->getProperties() as $property):
33+
array_push($this->array, $property->name);
34+
endforeach;
35+
$this->array = array_merge($this->array, $array);
36+
}
37+
38+
public function rewind() {
39+
$this->position = 0;
40+
}
41+
42+
public function current() {
43+
return $this->array[$this->position];
44+
}
45+
46+
public function key() {
47+
return $this->position;
48+
}
49+
50+
public function next() {
51+
++$this->position;
52+
}
53+
54+
public function valid() {
55+
return isset($this->array[$this->position]);
56+
}
57+
58+
protected function setAttribute($name, $value)
59+
{
60+
$this->$name = $value;
61+
}
62+
63+
protected function setAttributes(array $values)
64+
{
65+
$model = (new \ReflectionClass(get_called_class()))->newInstanceArgs();
66+
67+
foreach ($values as $key => $val):
68+
if (!$model->hasProperty($key)) {
69+
throw new \Exception($key. ' is not a property of '.$model->getClass());
70+
}
71+
endforeach;
72+
73+
foreach ($model as $key => $property):
74+
(array_key_exists($property, $values)) ?
75+
$this->setAttribute($property, $values[$property]) : '';
76+
endforeach;
77+
return true;
78+
}
79+
80+
public function load($post)
81+
{
82+
if(!is_array($post)) {
83+
return false;
84+
}
85+
$this->setAttributes($post);
86+
return true;
87+
}
88+
89+
public function hasProperty($name)
90+
{
91+
$re = false;
92+
foreach ($this as $property):
93+
($name == $property) ? $re = true : '';
94+
endforeach;
95+
return $re;
96+
}
97+
98+
}

0 commit comments

Comments
 (0)