Structuring the framework + startkit

This commit is contained in:
Jordy van Zeeland 2024-08-15 16:10:53 +02:00
parent ebbdb40cd3
commit 2400f8bd21
9 changed files with 65 additions and 14 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
vendor
composer.lock

View File

@ -0,0 +1,11 @@
<?php
use sealandia\framework\Template;
class Welcome{
public function index(){
$template = new Template();
return $template->render('welcome', []);
}
}

View File

@ -1,7 +1,8 @@
{ {
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"sealandia\\": "./" "sealandia\\": "./",
"sealandia\\framework\\": "framework/"
} }
}, },
"require": { "require": {

View File

@ -1,14 +1,18 @@
<?php <?php
namespace sealandia\framework;
class Router{ class Router{
private $uri;
/** /**
* Constructor method * Constructor method
* Executed when an instance of the Router class is created * Executed when an instance of the Router class is created
*/ */
public function __construct(){ public function __construct(){
$uri = parse_url($_SERVER['REQUEST_URI'])['path']; $this->uri = parse_url($_SERVER['REQUEST_URI'])['path'];
} }
/** /**
@ -24,7 +28,22 @@ class Router{
*/ */
if(array_key_exists($this->uri, $routes)){ if(array_key_exists($this->uri, $routes)){
require $routes[$uri]; list($controller, $method) = explode('@', $routes[$this->uri]);
$controllerPath = __DIR__ . '/../app/controllers/' . $controller . '.php';
if(file_exists($controllerPath)){
require_once $controllerPath;
$controller = new $controller();
if(method_exists($controller, $method)){
$controller->$method();
}else{
$this->abort();
}
}else{
$this->abort();
}
}else{ }else{
$this->abort(); $this->abort();
} }
@ -46,13 +65,4 @@ class Router{
die(); die();
} }
} }
// $routes = [
// '/' => 'controllers/index.php',
// '/about' => 'controllers/about.php',
// '/contact' => 'controllers/contact.php',
// ];
// $router = Router::routeToController($routes);
?> ?>

View File

@ -1,5 +1,7 @@
<?php <?php
namespace sealandia\framework;
class Template{ class Template{
/** /**
@ -15,7 +17,7 @@ class Template{
*/ */
extract($args, EXTR_SKIP); extract($args, EXTR_SKIP);
require "views/{$code}.view.php"; require "views/{$tmpname}.view.php";
} }
} }

View File

@ -5,5 +5,5 @@ namespace sealandia;
session_start(); session_start();
require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/routes/routes.php';
?> ?>

View File

@ -0,0 +1,9 @@
<?php
use sealandia\framework\Router;
$routes = [
'/' => 'Welcome@index',
];
$router = (new Router)->routeToController($routes);

8
views/404.php Normal file
View File

@ -0,0 +1,8 @@
<html>
<head>
<title>Sealandia framework</title>
</head>
<body>
<h1>Page not found</h1>
</body>
</html>

8
views/welcome.view.php Normal file
View File

@ -0,0 +1,8 @@
<html>
<head>
<title>Sealandia framework</title>
</head>
<body>
<h1>Welcome to Sealandia</h1>
</body>
</html>