1. Go to this page and download the library: Download simplon/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/ */
simplon / core example snippets
$appContext = new AppContext();
$components = new ComponentsCollection();
$components->add(new FooRegistry($appContext());
$authContainer = new AuthContainer();
$middleware = new MiddlewareCollection();
$middleware->add(new AuthMiddleware($authContainer, $components));
(new Core())->run($components, $middleware);
namespace App\Components\Auth\Managers;
use App\Components\Auth\AuthRoutes;
use App\Components\Auth\Data\AuthSessionUser;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Simplon\Core\Interfaces\AuthUserInterface;
use Simplon\Core\Middleware\Auth\AuthContainer;
/**
* @package App\Components\Auth\Managers
*/
class AuthViewContainer extends AuthContainer
{
/**
* @param ServerRequestInterface $request
*
* @return null|AuthUserInterface
*/
public function fetchUser(ServerRequestInterface $request): ?AuthUserInterface
{
if (!empty($_SESSION['session']))
{
return new AuthSessionUser($_SESSION['session']);
}
return null;
}
/**
* @return callable|null
*/
protected function getOnError(): ?callable
{
return function (ResponseInterface $response) {
if (empty($response->getHeaderLine('Location')))
{
$response = $response->withAddedHeader('Location', AuthRoutes::toSignIn());
}
return $response;
};
}
}
namespace App\Components\Auth\Managers;
use App\Components\Auth\AuthRoutes;
use App\Components\Auth\Data\AuthRestUser;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Simplon\Core\Interfaces\AuthUserInterface;
use Simplon\Core\Middleware\Auth\AuthContainer;
use Simplon\Mysql\Mysql;
use Simplon\Mysql\MysqlException;
/**
* @package App\Components\Auth\Managers
*/
class AuthRestContainer extends AuthContainer
{
/**
* @var Mysql
*/
private $mysql;
/**
* @param Mysql $mysql
*/
public function __construct(Mysql $mysql)
{
$this->mysql = $mysql;
}
/**
* @param ServerRequestInterface $request
*
* @return null|AuthUserInterface
* @throws MysqlException
*/
public function fetchUser(ServerRequestInterface $request): ?AuthUserInterface
{
if ($bearer = $this->fetchAuthBearer($request))
{
list($token, $secret) = explode(':', $bearer);
$query = '
-- noinspection SqlDialectInspection
select * from ' . AuthStore::TABLE_NAME . ' where ' . AuthModel::COLUMN_TOKEN . ' = :token
';
if ($row = $this->mysql->fetchRow($query, ['token' => $token]))
{
$authUser = new AuthRestUser($row);
if ($secret)
{
$authUser->validateSecret($secret);
}
return $authUser;
}
}
return null;
}
/**
* @return callable|null
*/
protected function getOnError(): ?callable
{
return function (ResponseInterface $response) {
if (empty($response->getHeaderLine('Location')))
{
$response = $response->withAddedHeader('Location', AuthRoutes::toSignIn());
}
return $response;
};
}
}
class SomeViewController extends ViewController
{
/**
* @param array $params
*
* @return ResponseViewData
*/
public function __invoke(array $params): ResponseViewData
{
// some code
//
// you can handle redirects
//
if($shouldRedirect)
{
$this->getFlashMessage()->setFlashSuccess('Some flash message');
return $this->redirect('/some/route');
}
//
// or respond with view data
//
return $this->respond(new SomeView());
}
/**
* @return SomeRegistry
*/
public function getRegistry(): SomeRegistry
{
return $this->registry;
}
}
class SomeRestController extends RestController
{
/**
* @param array $params
*
* @return ResponseViewData
*/
public function __invoke(array $params): ResponseRestData
{
// some code
//
// respond with array data which will be
// transformed into JSON
//
return $this->respond(['foo' => 'bar']);
}
/**
* @return SomeRegistry
*/
public function getRegistry(): SomeRegistry
{
return $this->registry;
}
}
/**
* @var FlashMessage $flash
* @var Locale $locale
* @var Device $device
*
* @var string $content
*/
use Simplon\Core\Views\FlashMessage;
use Simplon\Core\Views\View;
use Simplon\Device\Device;
use Simplon\Locale\Locale;
namespace App;
use Simplon\Core\Utils\Form\BaseForm;
use Simplon\Form\Data\FormField;
use Simplon\Form\Data\Rules\RequiredRule;
use Simplon\Form\Data\Rules\EmailRule;
class CreateForm extends BaseForm
{
const NAME = 'name';
const EMAIL = 'email';
/**
* @return FormField[]
*/
protected function buildFields(): array
{
return [
$this->getName(),
$this->getEmail(),
];
}
/**
* @return FormField
*/
private function getName(): FormField
{
return (new FormField(self::NAME))->addRule(new RequiredRule());
}
/**
* @return FormField
*/
private function getEmail(): FormField
{
return (new FormField(self::EMAIL))->addRule(new EmailRule());
}
}
namespace App;
use App\CreateFormFields;
use Simplon\Core\Utils\Form\BaseFormView;
use Simplon\Form\FormError;
use Simplon\Form\View\Elements\DropDownElement;
use Simplon\Form\View\Elements\InputTextElement;
use Simplon\Form\View\FormViewBlock;
use Simplon\Form\View\FormViewRow;
class CreateFormView extends BaseFormView
{
/**
* @return FormViewBlock[]
* @throws FormError
*/
protected function getBlocks(): array
{
return [
$this->buildFormViewBlock(self::BLOCK_DEFAULT)
->addRow(
$this->>buildFormViewRow()
->autoColumns($this->getNameElement())
->autoColumns($this->getEmailElement())
),
];
}
/**
* @return string
*/
protected function getSubmitLabel(): string
{
return $this->getLocale()->get('form-create-submit-label');
}
/**
* @return InputTextElement
* @throws FormError
*/
private function getNameElement(): InputTextElement
{
$element = new InputTextElement($this->getFields()->get(CreateFormFields::NAME));
$element
->setLabel($this->getLocale()->get('form-create-name-label'))
->setPlaceholder($this->getLocale()->get('form-create-name-placeholder'))
;
return $element;
}
/**
* @return InputTextElement
* @throws FormError
*/
private function getEmailElement(): InputTextElement
{
$element = new InputTextElement($this->getFields()->get(CreateFormFields::EMAIL));
$element
->setLabel($this->getLocale()->get('form-create-email-label'))
->setPlaceholder($this->getLocale()->get('form-create-email-placeholder'))
;
return $element;
}
}
namespace App;
use Simplon\Core\Utils\Form\ViewWithForm;
class CreateView extends ViewWithForm
{
/**
* @return string
*/
protected function getTemplate(): string
{
return __DIR__ . '/CreateTemplate.phtml';
}
}
/**
* @var Locale $locale
* @var FlashMessage $flash
* @var Device $device
*
* @var FormView $formView
*/
use App\AppContext;
use Simplon\Core\Views\FlashMessage;
use Simplon\Core\Views\View;
use Simplon\Device\Device;
use Simplon\Form\View\FormView;
use Simplon\Locale\Locale;
/**
* @var Locale $locale
* @var FormView $formView
*/
use App\CreateFormView;
use Simplon\Form\View\FormView;
use Simplon\Locale\Locale;
namespace App;
use App\CreateForm;
use App\CreateFormView;
use App\CreateView;
use Simplon\Core\Controllers\ViewController;
use Simplon\Core\Utils\Form\FormWrapper;
use Simplon\Core\Data\ResponseViewData;
use Simplon\Form\FormFields;
class CreateViewController extends ViewController
{
/**
* @param array $params
*
* @return ResponseViewData
* @throws \Simplon\Form\FormError
*/
public function __invoke(array $params): ResponseViewData
{
$formWrapper = $this->buildFormWrapper(
new CreateForm($this->getLocale())
);
if ($formWrapper->getValidator()->validate()->isValid())
{
// do something with the form data
return $this->redirect('/some/other/url');
}
$formView = new CreateFormView($this->getLocale(), $formWrapper->getFields());
return $this->respond(
new CreateView($this->getCoreViewData(), $formView)
);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.