PHP code example of ricardoper / slim4-twig-skeleton
1. Go to this page and download the library: Download ricardoper/slim4-twig-skeleton 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/ */
use App\Controllers\Demo\AddressesController;
use App\Controllers\Demo\HelloController;
use App\Controllers\Demo\HomeController;
use Slim\App;
/**
* @var $app App
*/
$app->get('/', [(new HomeController()), 'index']);
$app->get('/flash', [(new HomeController()), 'flash']);
$app->get('/dump', [(new HomeController()), 'dump']);
$app->get('/hello/{name}', [(new HelloController()), 'index'])->setName('namedRoute');
$app->get('/addresses', [(new AddressesController()), 'list']);
$app->get('/addresses/pdo', [(new AddressesController()), 'pdo']);
use App\Kernel\Abstracts\ControllerAbstract;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class HomeController extends ControllerAbstract
{
/**
* Index Action
*
* @param Request $request
* @param Response $response
* @return Response
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function index(Request $request, Response $response): Response
{
unset($request, $response);
return $this->render('Demo/Home/index.twig');
}
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
class ExampleMiddleware implements Middleware
{
/**
* Process an incoming server request.
*
* Processes an incoming server request in order to produce a response.
* If unable to produce the response itself, it may delegate to the provided
* request handler to do so.
*
* @param Request $request
* @param RequestHandler $handler
* @return Response
*/
public function process(Request $request, RequestHandler $handler): Response
{
$response = $handler->handle($request);
$response = $response->withHeader('X-Example', 'Middleware');
return $response;
}
}
use App\Middlewares\Demo\ExampleMiddleware;
return [
'example' => ExampleMiddleware::class,
];
use App\Kernel\Interfaces\ResponseEmitterInterface;
use Psr\Http\Message\ResponseInterface;
class JsonResponseEmitter implements ResponseEmitterInterface
{
/**
* Send the response to the client
*
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function emit(ResponseInterface $response): ResponseInterface
{
$response = $response
->withHeader('Content-Type', 'application/json; charset=UTF-8');
return $response;
}
}
use App\Emitters\JsonResponseEmitter;
return [
'json' => JsonResponseEmitter::class,
];
use App\Kernel\Abstracts\ModelAbstract;
use PDO;
class AddressesModel extends ModelAbstract
{
/**
* Get Last Addresses with Pdo
*
* @param int $limit
* @return array
*/
public function getLastWithPdo(int $limit = 25): array
{
/** @var $db PDO */
$db = $this->getDb()->pdo;
$sql = 'SELECT `address`.`address_id`,`address`.`address`,`address`.`address2`,`address`.`district`,`city`.`city`,`address`.`postal_code`,`address`.`phone` FROM `address` ';
$sql .= 'LEFT JOIN `city` ON `address`.`city_id` = `city`.`city_id` ';
$sql .= 'ORDER BY `address_id` DESC LIMIT 10';
return $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}
use App\Kernel\Interfaces\ServiceProviderInterface;
use Pimple\Container;
class ExampleServiceProvider implements ServiceProviderInterface
{
/**
* Service register name
*/
public function name(): string
{
return 'example';
}
/**
* Register new service on dependency container
*
* @param Container $container
* @return mixed
*/
public function register(Container $container)
{
return function (Container $c) {
unset($c);
return new Example();
};
}
}
use App\Services\Demo\ExampleServiceProvider;
return [
'example' => ExampleServiceProvider::class,
];
use App\Handlers\ShutdownHandler;
use Slim\Handlers\ErrorHandler;
return [
// Handlers //
'errorHandler' => ErrorHandler::class,
'shutdownHandler' => ShutdownHandler::class,
use App\Services\Database\DatabaseServiceProvider;
return [
'database' => DatabaseServiceProvider::class,
];
text
LOG_ERRORS | logErrors (bool) - Enable/Disable logging.
LOG_ERRORS_DETAILS | logErrorDetails (bool) - Enable/disable extra details in the logging file.
LOG_TO_OUTPUT | logToOutput (bool) - `true` to output the logs in console, `false` to output logs in file.
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.