├── .gitignore
├── public
└── index.php
├── README.md
├── src
├── Routes
│ └── index.php
├── Views
│ └── index.php
├── Controller.php
├── Models
│ └── Journal.php
├── Controllers
│ └── HomeController.php
└── Router.php
└── composer.json
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor/
2 |
--------------------------------------------------------------------------------
/public/index.php:
--------------------------------------------------------------------------------
1 | get('/', HomeController::class, 'index');
9 |
10 | $router->dispatch();
11 |
--------------------------------------------------------------------------------
/src/Views/index.php:
--------------------------------------------------------------------------------
1 |
Welcome to Simple PHP MVC Starter!
2 |
3 |
4 |
5 | - = $journal->name ?> (= $journal->publishedYear ?>)
6 |
7 |
--------------------------------------------------------------------------------
/src/Controller.php:
--------------------------------------------------------------------------------
1 | name = $name;
13 | $this->publishedYear = $publishedYear;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/Controllers/HomeController.php:
--------------------------------------------------------------------------------
1 | render('index', ['journals' => $journals]);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/Router.php:
--------------------------------------------------------------------------------
1 | routes[$method][$route] = ['controller' => $controller, 'action' => $action];
13 | }
14 |
15 | public function get($route, $controller, $action)
16 | {
17 | $this->addRoute($route, $controller, $action, "GET");
18 | }
19 |
20 | public function post($route, $controller, $action)
21 | {
22 | $this->addRoute($route, $controller, $action, "POST");
23 | }
24 |
25 | public function dispatch()
26 | {
27 | $uri = strtok($_SERVER['REQUEST_URI'], '?');
28 | $method = $_SERVER['REQUEST_METHOD'];
29 |
30 | if (array_key_exists($uri, $this->routes[$method])) {
31 | $controller = $this->routes[$method][$uri]['controller'];
32 | $action = $this->routes[$method][$uri]['action'];
33 |
34 | $controller = new $controller();
35 | $controller->$action();
36 | } else {
37 | throw new \Exception("No route found for URI: $uri");
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------