Skip to content

Commit dc9050d

Browse files
committed
Added Composer Autoload PSR-0
1 parent 5c5c867 commit dc9050d

File tree

9 files changed

+65
-51
lines changed

9 files changed

+65
-51
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
},
1919
"autoload":{
2020
"psr-0":{
21-
"Lib": "/lib/",
22-
"Model": "/models/"
21+
"": ""
2322
}
2423
}
2524
}

config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use lib\Config;
4+
35
// DB Config
46
Config::write('db.host', 'localhost');
57
Config::write('db.port', '');

lib/Config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
namespace lib;
4+
35
// Configuration Class
46
class Config {
57
static $confArray;

lib/Core.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

3+
namespace lib;
4+
5+
use lib\Config;
6+
use PDO;
7+
38
class Core {
49
public $dbh; // handle of the db connexion
510
private static $instance;

models/Stuff.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<?php
22

3+
namespace models;
4+
use lib\Core;
5+
36
class Stuff {
47

58
protected $core;
69

710
function __construct() {
8-
$this->core = Core::getInstance();
11+
//$this->core = \lib\Core::getInstance();
912
}
1013

1114
// Get all stuff
@@ -22,4 +25,8 @@ public function getAllStuff() {
2225
}
2326
return $r;
2427
}
28+
29+
public function setStuff() {
30+
return "hello world!!!";
31+
}
2532
}

models/User.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*
66
*/
77

8+
namespace models;
9+
use lib\Core;
810

911
class User {
1012

public/index.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<?php
22

3-
require '../lib/Config.php';
4-
require '../config.php';
5-
require '../lib/Core.php';
6-
73
require '../vendor/autoload.php';
4+
require '../config.php';
85

96
// Setup custom Twig view
107
$twigView = new \Slim\Views\Twig();

readme.md

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,29 @@ Example of class:
3434
Stuff.php
3535

3636
```php
37-
class Stuff {
38-
39-
private $core;
40-
41-
function __construct() {
42-
$this->core = Core::getInstance();
43-
}
44-
45-
// Get all stuff
46-
public function getAllStuff() {
47-
$r = array();
48-
49-
$sql = "SELECT * FROM stuff";
50-
$stmt = $this->core->dbh->prepare($sql);
51-
52-
if ($stmt->execute()) {
53-
$r = $stmt->fetchAll(PDO::FETCH_ASSOC);
54-
} else {
55-
$r = 0;
56-
}
57-
return $r;
58-
}
59-
}
37+
class Stuff {
38+
39+
protected $core;
40+
41+
function __construct() {
42+
$this->core = Core::getInstance();
43+
}
44+
45+
// Get all stuff
46+
public function getAllStuff() {
47+
$r = array();
48+
49+
$sql = "SELECT * FROM stuff";
50+
$stmt = $this->core->dbh->prepare($sql);
51+
52+
if ($stmt->execute()) {
53+
$r = $stmt->fetchAll(PDO::FETCH_ASSOC);
54+
} else {
55+
$r = 0;
56+
}
57+
return $r;
58+
}
59+
}
6060
```
6161

6262
### public/
@@ -75,25 +75,25 @@ Example of router file:
7575
stuff.router.php
7676

7777
```php
78-
// Get stuff
79-
$app->get('/stuff', function () use ($app) {
80-
echo 'This is a GET route';
81-
});
82-
83-
//Create user
84-
$app->post('/stuff', function () use ($app) {
85-
echo 'This is a POST route';
86-
});
87-
88-
// PUT route
89-
$app->put('/stuff', function () {
90-
echo 'This is a PUT route';
91-
});
92-
93-
// DELETE route
94-
$app->delete('/stuff', function () {
95-
echo 'This is a DELETE route';
96-
});
78+
// Get stuff
79+
$app->get('/stuff', function () use ($app) {
80+
echo 'This is a GET route';
81+
});
82+
83+
//Create user
84+
$app->post('/stuff', function () use ($app) {
85+
echo 'This is a POST route';
86+
});
87+
88+
// PUT route
89+
$app->put('/stuff', function () {
90+
echo 'This is a PUT route';
91+
});
92+
93+
// DELETE route
94+
$app->delete('/stuff', function () {
95+
echo 'This is a DELETE route';
96+
});
9797
```
9898

9999
### templates/

routers/index.router.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// GET index route
44
$app->get('/', function () use ($app) {
5-
$hello = "hello world!";
6-
//echo $hello;
5+
$oStuff = new models\Stuff();
6+
$hello = $oStuff->setStuff();
77
$app->render('index.html', array('hello' => $hello));
88
});

0 commit comments

Comments
 (0)