1. Go to this page and download the library: Download tobento/app-view 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/ */
tobento / app-view example snippets
use Tobento\App\AppFactory;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()
->dir(realpath(__DIR__.'/../'), 'root')
->dir(realpath(__DIR__.'/app/'), 'app')
->dir($app->dir('app').'config', 'config', group: 'config')
->dir($app->dir('root').'vendor', 'vendor')
->dir($app->dir('app').'views', 'views', group: 'views')
->dir($app->dir('root').'public', 'public');
// Adding boots
$app->boot(\Tobento\App\View\Boot\View::class);
// Run the app
$app->run();
use Tobento\App\AppFactory;
use Tobento\Service\View\ViewInterface;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()
->dir(realpath(__DIR__.'/../'), 'root')
->dir(realpath(__DIR__.'/app/'), 'app')
->dir($app->dir('app').'config', 'config', group: 'config')
->dir($app->dir('root').'vendor', 'vendor')
->dir($app->dir('app').'views', 'views', group: 'views')
->dir($app->dir('root').'public', 'public');
// Adding boots
$app->boot(\Tobento\App\View\Boot\View::class);
$app->booting();
$view = $app->get(ViewInterface::class);
$content = $view->render(view: 'about', data: []);
// or using the app macro:
$content = $app->renderView(view: 'about', data: []);
// Run the app
$app->run();
use Tobento\Service\View\ViewInterface;
class SomeService
{
public function __construct(
protected ViewInterface $view,
) {}
}
use Tobento\App\Boot;
use Tobento\App\View\Boot\View;
class AnyServiceBoot extends Boot
{
public const BOOT = [
// you may ensure the view boot.
View::class,
];
public function boot(View $view)
{
$content = $view->render(view: 'about', data: []);
}
}
use Tobento\Service\Responser\ResponserInterface;
use Psr\Http\Message\ResponseInterface;
class SomeHttpController
{
public function index(ResponserInterface $responser): ResponseInterface
{
return $responser->render(view: 'register', data: []);
}
}
// by variable:
$htmlLang
$locale
$routeName
// by view get method:
$htmlLang = $view->get('htmlLang', 'en');
$locale = $view->get('locale', 'en');
$routeName = $view->get('routeName', '');
use Tobento\Service\Language\LanguagesInterface;
if ($this->app->has(LanguagesInterface::class)) {
$locale = $this->app->get(LanguagesInterface::class)->current()->locale();
$view->with('htmlLang', str_replace('_', '-', $locale));
$view->with('locale', $locale);
}
use Tobento\Service\Routing\RouterInterface;
if ($this->app->has(RouterInterface::class)) {
$matchedRoute = $this->app->get(RouterInterface::class)->getMatchedRoute();
$view->with('routeName', $matchedRoute?->getName() ?: '');
}
use Tobento\App\Boot;
use Tobento\Service\View\ViewInterface;
class SomeGlobalViewDataBoot extends Boot
{
public function boot()
{
// only add if view is requested:
$this->app->on(ViewInterface::class, function(ViewInterface $view) {
// using the with method:
$view->with(name: 'someVariable', value: 'someValue');
// using the data method:
$view->data([
'someVariable' => 'someValue',
]);
});
}
}
use Tobento\App\AppInterface;
var_dump($view->app() instanceof AppInterface);
// bool(true)
use Tobento\App\AppFactory;
use Tobento\Service\Menu\MenusInterface;
use Tobento\Service\Menu\MenuInterface;
use Tobento\Service\View\ViewInterface;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()
->dir(realpath(__DIR__.'/../'), 'root')
->dir(realpath(__DIR__.'/app/'), 'app')
->dir($app->dir('app').'config', 'config', group: 'config')
->dir($app->dir('root').'vendor', 'vendor')
->dir($app->dir('app').'views', 'views', group: 'views')
->dir($app->dir('root').'public', 'public');
// Adding boots
$app->boot(\Tobento\App\View\Boot\View::class);
// no need to boot as already loaded by the view boot:
// $app->boot(\Tobento\App\View\Boot\Menus::class);
$app->booting();
// Get the menus:
$menus = $app->get(MenusInterface::class);
// View menu macro:
$view = $app->get(ViewInterface::class);
var_dump($view->menu('main') instanceof MenuInterface);
// bool(true)
// Run the app
$app->run();
use Tobento\App\Boot;
use Tobento\App\View\Boot\Menus;
class AnyServiceBoot extends Boot
{
public const BOOT = [
// you may ensure the menus boot.
Menus::class,
];
public function boot(Menus $menus)
{
$menu = $menus->menu('main');
$menu->link('https://example.com/foo', 'Foo')->id('foo');
}
}
use Tobento\App\Boot;
use Tobento\Service\Menu\MenusInterface;
class AnyServiceBoot extends Boot
{
public function boot()
{
$this->app->on(MenusInterface::class, function(MenusInterface $menus) {
$menu = $menus->menu('main');
$menu->link('https://example.com/foo', 'Foo')->id('foo');
});
}
}
use Tobento\App\AppFactory;
use Tobento\Service\Form\FormFactoryInterface;
use Tobento\Service\Form\Form;
use Tobento\Service\View\ViewInterface;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()
->dir(realpath(__DIR__.'/../'), 'root')
->dir(realpath(__DIR__.'/app/'), 'app')
->dir($app->dir('app').'config', 'config', group: 'config')
->dir($app->dir('root').'vendor', 'vendor')
->dir($app->dir('app').'views', 'views', group: 'views')
->dir($app->dir('root').'public', 'public');
// Adding boots
$app->boot(\Tobento\App\View\Boot\View::class);
$app->boot(\Tobento\App\View\Boot\Form::class);
$app->booting();
// Get the form factory:
$formFactory = $app->get(FormFactoryInterface::class);
// View form macro:
$view = $app->get(ViewInterface::class);
$form = $view->form();
// var_dump($form instanceof Form);
// bool(true)
// Run the app
$app->run();
$token = $view->data()->get('csrfToken');
use Tobento\Service\Responser\ResponserInterface;
use Tobento\Service\Requester\RequesterInterface;
use Psr\Http\Message\ResponseInterface;
class RegisterController
{
public function index(ResponserInterface $responser): ResponseInterface
{
// set the key corresponding to your form field name:
$responser->messages()->add('info', 'Some info message', key: 'firstname');
return $responser->render(view: 'register');
}
public function register(
RequesterInterface $requester,
ResponserInterface $responser,
): ResponseInterface {
// validate request data:
//$requester->input();
// add message on error:
$responser->messages()->add('error', 'Message error', key: 'firstname');
// redirect - messages and input data will be flashed:
return $responser->redirect(
uri: 'uri',
)->withInput($requester->input()->all());
}
}
use Tobento\Service\Message\MessagesInterface;
use Tobento\Service\Message\Messages;
class SomeHttpController
{
public function index(ViewInterface $view): string
{
$messages = new Messages();
$messages->add(level: 'error', message: 'Error message');
return $view->render(
view: 'register',
data: [
'messages' => $messages,
],
);
}
}
use Tobento\Service\Responser\ResponserInterface;
use Psr\Http\Message\ResponseInterface;
class SomeHttpController
{
public function index(ResponserInterface $responser): ResponseInterface
{
$responser->messages()->add('info', 'Message info');
$responser->messages()->add('success', 'Message success');
$responser->messages()->add('error', 'Message error');
$responser->messages()->add('warning', 'Message warning');
// if a key is specified, the message will not be rendered,
// as it may belong to a form field.
$responser->messages()->add('error', 'Message error', key: 'firstname');
return $responser->render(view: 'register');
}
}
use Tobento\Service\Responser\ResponserInterface;
use Psr\Http\Message\ResponseInterface;
class SomeHttpController
{
public function index(ResponserInterface $responser): ResponseInterface
{
return $responser->render(
view: 'exception/error',
data: [
'code' => '403',
'message' => 'Forbidden',
],
code: 403,
);
}
}
use Tobento\App\Boot;
class SomeThemeBoot extends Boot
{
public function boot()
{
// add a new view directory to load views from
// with a higher priority as the default.
$this->app->dirs()->dir(
dir: $this->app->dir('app').'/theme/',
name: 'theme',
group: 'views',
priority: 500, // default is 100
);
}
}
$view->asset('assets/css/my-app.css');
use Tobento\App\Boot;
use Tobento\Service\View\ViewInterface;
use Tobento\Service\View\AssetsHandlerInterface;
class SomeThemeBoot extends Boot
{
public function boot()
{
$this->app->on(ViewInterface::class, function(ViewInterface $view) {
$view->assets()->setAssetsHandler(
assetsHandler: $assetsHandler, // AssetsHandlerInterface
);
});
}
}
date
exception/error.php
exception/error.xml.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.