1. Go to this page and download the library: Download cevantime/sherpa-core 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/ */
cevantime / sherpa-core example snippets
// index.php
use Zend\Diactoros\ServerRequestFactory;
$request = ServerRequestFactory::fromGlobals();
use Sherpa\Kernel\Kernel;
// ...
$app = new Kernel();
use Zend\Diactoros\Response\HtmlResponse;
// ...
$app->pipe(function(){
return new HtmlResponse("Hello Sherpa !");
});
$response = $app->handle($request);
use Zend\Diactoros\Response\SapiEmitter;
// ...
(new SapiEmitter())->emit($response);
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
$kernel->pipe(function(ServerRequestInterface $request, RequestHandlerInterface $handler) {
// ...
});
$kernel->pipe(function(ServerRequestInterface $request, RequestHandlerInterface $handler) {
// check if user tries to access an admin page and check if he is admin
if(preg_match($request->getUri()->getPath(), '~^/admin~')
&& ! $request->getAttribute('user')->isAdmin()) {
// if not, send an error
return (new Response('php://memory'))
->withStatus(403, 'You are not allowed to enter !';
}
// otherwise, let the process continue and the next middleware proceed
return $handler->handle($request);
});
$kernel->pipe(function(){ return new Response(...); }, 50);
use function DI\get;
use function DI\create;
use Psr\Log\LoggerInterface;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Psr\Container\ContainerInterface;
//...
$builder = $kernel->getContainerBuilder();
$builder->addDefinitions([
// create definition directly...
'log.folder' => 'var/log',
// ... or using a callback function...
StreamHandler::class => function(ContainerInterface $container) {
return new StreamHandler($container->get('log.folder');
},
// ... or using configuration methods that come with php di
LoggerInterface::class => create(Logger::class)
->constructor('mylogger')
->method('pushHandler', get(StreamHandler::class))
]);
use App\UserRepository;
use Psr\Log\LoggerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as Handler;
use Psr\Http\Message\ResponseInterface as Response;
class UserMiddleware implements MiddlewareInterface
{
protected $userRepository;
protected $logger;
/**
* @param UserRepository $userRepository is auto-injected !
* @param LoggerInterface $logger hase been configure to inject an instance of Logger
* /
public function __construct(UserRepository $userRepository, LoggerInterface $logger)
{
$this->userRepository = $userRepository;
$this->logger = $logger;
}
public function process(Request $request, Handler $handler): Response
{
$user = $this->userRepository->findBy(['id' => $_SESSION['user_id'] ?? 0]);
if($user) {
$request->setAttribute('user', $user);
} else {
$this->logger->log('info', 'No user found in session');
}
return $handler->handle($request);
}
}
$kernel->pipe(UserMiddleware::class);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.