An exercise on how to make an MVC framework from (not really) scratch.
The framework uses the package vlucas/phpdotenv for environment secret's management.
Environment are stored in the .env
file. Use the ENV[$key]
super global or the env($key, $default = null)
global function to access the individual environment values.
A simple IoC container.
Objects can be bound to the container:
Application::bind(
$object,
$name // opcional
);
For complex objects a Closure
can be used:
Application::bind(function(){
return (new Object())
->doSomethingElse();
});
Objects can be resolved:
Or using the helper:
Application::resolve($name);
Here's the list of objects bound by the Application
:
- env
- config
- template
- router
The can be resolved using the keyword:
Application::resolve('config');
For routes the package bramus/router is used.
Add routes in app/routes.php
calling the verb method, with the path as a first parameter and a closure as a second parameter:
use Core\Application;
$router = Application::resolve('router');
$router->get('/', function() {
// your code here
});
Methods available:
- get
- post
- put
- patch
- delete
Views use the package thephpleague/plates.
You can add views inside the resources\Views
folder.
To render the view the view
helper can be used. The first parameter must be the name of view's file (without the .php
) and the second parameter is an array with available arguments that will become available inside the view:
use Core\Application;
$router = Application::resolve('router');
$router->get('/', function() {
view('home', [
'foo' => 'var',
]);
});
Configuration values can be stored in the App/config.php
file. Environment values can be used inside for composing values:
return [
'title' => env('TITLE', 'MiniFramework 2'),
];
To access the config
the helper can be used.
$this->get('/', function (){
$title = config('title');
});
- Middleware
- Models
- Request
- Singletons for the container
- 404 view