1. Go to this page and download the library: Download robertwesner/simple-mvc-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
robertwesner / simple-mvc-php example snippets
Route::post('/api/login', function (Request $request) {
// Reads either Query or JSON-Body Parameter
$password = $request->getRequestParameter('password');
if ($password === null) {
return Route::response('Bad Request', 400);
}
// ...
return Route::json([
'success' => $success,
]);
});
Route::post('/api/logout', function () {
// ...
});
// Also able to read URI parameters
Route::get('/api/users/(?<userId>\d+)', function (Request $request) {
$userId = $request->getUriParameter('userId'); // Returns numeric userId from capture group
// ...
});
// 404 page, FALLBACK will be called when no other route matches
Route::get(Route::FALLBACK, function (Request $request) {
return Route::render('404.twig');
});
final class UserService
{
// ...
}
readonly class UserController
{
public function __construct(
private UserService $userService,
) {}
public function all(): ResponseInterface
{
// ...
}
public function get(Request $request): ResponseInterface
{
// ...
}
public function create(Request $request): ResponseInterface
{
// ...
}
public function delete(Request $request): ResponseInterface
{
// ...
}
}
// Autowired service class (AuthenticationService) inside Route
// Note: this ationService $authenticationService) {
// ...
});
Configuration::CONTAINER
// Either let the container do all the heavy lifting via class names,
// MySQLEntityManager would be automatically instantiated by the container.
// This is necessary for usage of interfaces, rather than implementations.
::instantiate(EntityManagerInterface::class, MySQLEntityManager::class)
// Or pass your own instance when necessary, since Bar is not to be autowired.
::register(FooInterface::class, new Bar('SOME VALUE'));
Configuration::BUNDLES
::load(FooBundle::class)
// Optionally with additional configuration of any type, depending on the bundle.
::load(BarBundle::class, ['faz' => 'baz']);