Example of usage
use Bluz\Proxy\Layout;
Layout::title('Homepage');
Layout::set('description', 'some page description');
/** * {@inheritdoc} * * @param string $module * @param string $controller * @param array $params * @return void */ protected function preDispatch($module, $controller, $params = array()) { // example of setup default title Layout::title("Bluz Skeleton"); // apply "remember me" function if (!$this->user() && !empty($_COOKIE['rToken']) && !empty($_COOKIE['rId'])) { // try to login try { Auth\Table::getInstance()->authenticateCookie($_COOKIE['rId'], $_COOKIE['rToken']); } catch (AuthException $e) { $this->getResponse()->setCookie('rId', '', 1, '/'); $this->getResponse()->setCookie('rToken', '', 1, '/'); } } parent::preDispatch($module, $controller, $params); }
/** * View User Profile * * @author Anton Shevchuk * @created 01.09.11 13:15 */ namespace Application; use Application\Users; use Bluz\Proxy\Layout; return function ($id = null) use($view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::title('User Profile'); // try to load profile of current user if (!$id && $this->user()) { $id = $this->user()->id; } /** * @var Users\Row $user */ $user = Users\Table::findRow($id); if (!$user) { throw new Exception('User not found', 404); } else { $view->user = $user; } };
/** * Build list of custom routers * * @author Anton Shevchuk * @created 12.06.12 12:27 */ namespace Application; use Bluz\Proxy\Layout; return function () use($view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::title('Routers Map'); Layout::setTemplate('dashboard.phtml'); Layout::breadCrumbs([$view->ahref('Dashboard', ['dashboard', 'index']), $view->ahref('System', ['system', 'index']), __('Routers Map')]); $routers = array(); foreach (new \GlobIterator(PATH_APPLICATION . '/modules/*/controllers/*.php') as $file) { $module = pathinfo(dirname(dirname($file->getPathname())), PATHINFO_FILENAME); $controller = pathinfo($file->getPathname(), PATHINFO_FILENAME); $reflection = $this->reflection($file->getPathname()); if ($route = $reflection->getRoute()) { if (!isset($routers[$module])) { $routers[$module] = array(); } $routers[$module][$controller] = ['route' => $route, 'params' => $reflection->getParams()]; } } $view->routers = $routers; };
<?php /** * Bootstrap of index module * * @author Anton Shevchuk * @created 07.07.11 18:03 * @return closure */ namespace Application; use Bluz\Proxy\Layout; return function ($a) { Layout::title("Test", Layout::POS_APPEND); return $a * 2; };
<?php /** * Disable view, like for backbone.js * * @author Anton Shevchuk * @created 22.08.12 17:14 */ namespace Application; use Bluz\Proxy\Layout; return function () { Layout::breadCrumbs([Layout::ahref('Test', ['test', 'index']), 'Without view']); return function () { }; };
/** * Do process * @return void */ protected function doProcess() { Logger::info("app:process:do"); $module = Request::getModule(); $controller = Request::getController(); $params = Request::getAllParams(); // try to dispatch controller try { // get reflection of requested controller $controllerFile = $this->getControllerFile($module, $controller); $reflection = $this->reflection($controllerFile); // check header "accept" for catch JSON(P) or XML requests, and switch presentation // it's some magic for AJAX and REST requests if ($produces = $reflection->getAccept() and $accept = $this->getRequest()->getAccept()) { // switch statement for $accept switch ($accept) { case Request::ACCEPT_HTML: // with layout // without additional presentation break; case Request::ACCEPT_JSON: $this->useJson(); break; case Request::ACCEPT_JSONP: $this->useJsonp(); break; case Request::ACCEPT_XML: $this->useXml(); break; default: // not acceptable MIME type throw new NotAcceptableException(); break; } } // check call method(s) if ($reflection->getMethod() && !in_array(Request::getMethod(), $reflection->getMethod())) { throw new NotAllowedException(join(',', $reflection->getMethod())); } // check HTML cache if ($reflection->getCacheHtml() && Request::getMethod() == Request::METHOD_GET) { $htmlKey = 'html:' . $module . ':' . $controller . ':' . http_build_query($params); if ($cachedHtml = Cache::get($htmlKey)) { Response::setBody($cachedHtml); return; } } // dispatch controller $dispatchResult = $this->dispatch($module, $controller, $params); } catch (RedirectException $e) { Response::setException($e); if (Request::isXmlHttpRequest()) { // 204 - No Content Response::setStatusCode(204); Response::setHeader('Bluz-Redirect', $e->getMessage()); } else { Response::setStatusCode($e->getCode()); Response::setHeader('Location', $e->getMessage()); } return null; } catch (ReloadException $e) { Response::setException($e); if (Request::isXmlHttpRequest()) { // 204 - No Content Response::setStatusCode(204); Response::setHeader('Bluz-Reload', 'true'); } else { Response::setStatusCode($e->getCode()); Response::setHeader('Refresh', '0; url=' . Request::getRequestUri()); } return null; } catch (\Exception $e) { Response::setException($e); // cast to valid HTTP error code // 500 - Internal Server Error $statusCode = 100 <= $e->getCode() && $e->getCode() <= 505 ? $e->getCode() : 500; Response::setStatusCode($statusCode); $dispatchResult = $this->dispatch(Router::getErrorModule(), Router::getErrorController(), array('code' => $e->getCode(), 'message' => $e->getMessage())); } if ($this->hasLayout()) { Layout::setContent($dispatchResult); $dispatchResult = Layout::getInstance(); } if (isset($htmlKey, $reflection)) { // @TODO: Added ETag header Cache::set($htmlKey, $dispatchResult(), $reflection->getCacheHtml()); Cache::addTag($htmlKey, $module); Cache::addTag($htmlKey, 'html'); Cache::addTag($htmlKey, 'html:' . $module); Cache::addTag($htmlKey, 'html:' . $module . ':' . $controller); } Response::setBody($dispatchResult); }
*/ /** * @namespace */ namespace Application; use Application\Categories; use Bluz\Proxy\Layout; use Bluz\Proxy\Messages; return function ($id = null) use($view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::setTemplate('dashboard.phtml'); Layout::headStyle($view->baseUrl('css/categories.css')); Layout::breadCrumbs([$view->ahref('Dashboard', ['dashboard', 'grid']), __('Categories')]); $categoriesTable = Categories\Table::getInstance(); $rootTree = $categoriesTable->getAllRootCategory(); if (count($rootTree) == 0) { Messages::addNotice('There are no categories'); return $view; } $view->rootTree = $rootTree; if (!$id) { $id = $rootTree[0]->id; } $view->branch = $id; $view->tree = $categoriesTable->buildTree($id); return $view; };
<?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\Controller\Helper; use Bluz\Application\Application; use Bluz\Controller\Controller; use Bluz\Proxy\Layout; /** * Switch layout * * @param $layout */ return function ($layout) { /** * @var Controller $this */ Application::getInstance()->useLayout(true); Layout::setTemplate($layout); };
<?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\View\Helper; use Bluz\Application\Application; use Bluz\Proxy\Layout; return function ($style = null, $media = 'all') { /** * @var Layout $this */ if (Application::getInstance()->hasLayout()) { return Layout::headStyle($style, $media); } else { // it's just alias to style() call return $this->style($style); } };
<?php /** * Debug bookmarklet * * @author Anton Shevchuk * @created 22.08.12 17:14 */ namespace Application; use Bluz\Controller\Controller; use Bluz\Proxy\Layout; /** * @privilege Info * * @return array */ return function () { /** * @var Controller $this */ Layout::title('Bookmarklets'); Layout::setTemplate('dashboard.phtml'); Layout::breadCrumbs([Layout::ahref('Dashboard', ['dashboard', 'index']), Layout::ahref('System', ['system', 'index']), __('Bookmarklets')]); $key = getenv('BLUZ_DEBUG_KEY') ?: 'BLUZ_DEBUG'; return ['key' => $key]; };
/** * Do process * * @return void */ protected function doProcess() { $module = Request::getModule(); $controller = Request::getController(); $params = Request::getParams(); // try to dispatch controller try { // dispatch controller $result = $this->dispatch($module, $controller, $params); } catch (ForbiddenException $e) { $result = $this->forbidden($e); } catch (RedirectException $e) { // redirect to URL $result = $this->redirect($e->getUrl()); } catch (\Exception $e) { $result = $this->error($e); } // setup layout, if needed if ($this->useLayout()) { // render view to layout // needed for headScript and headStyle helpers Layout::setContent($result->render()); Response::setBody(Layout::getInstance()); } else { Response::setBody($result); } }
* Created by PhpStorm. * User: gunko * Date: 9/14/15 * Time: 11:04 AM */ namespace Application; use Bluz\Proxy\Layout; use Bluz\Proxy\Request; use Bluz\Proxy\Response; return function () use($view, $module, $controller) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::setTemplate('dashboard.phtml'); Layout::breadCrumbs([$view->ahref('Dashboard', ['dashboard', 'index']), __('Musician')]); $countCol = Request::getParam('countCol'); if ($countCol != null) { Response::setCookie("countCol", $countCol, time() + 3600, '/'); } else { $countCol = Request::getCookie('countCol', 4); } $grid = new Musician\Grid(); $lnCol = (int) (12 / $countCol); $view->countCol = $countCol; $view->col = $lnCol; $grid->setModule($module); $grid->setController($controller); $view->grid = $grid; };
<?php /** * PHP Info Wrapper * * @author Anton Shevchuk * @created 22.08.12 17:14 */ namespace Application; use Bluz\Proxy\Layout; return function () use($view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::title('PHP Info'); Layout::setTemplate('dashboard.phtml'); Layout::breadCrumbs([$view->ahref('Dashboard', ['dashboard', 'index']), $view->ahref('System', ['system', 'index']), __('PHP Info')]); };
<?php /** * Bootstrap example * * @author Anton Shevchuk * @created 12.06.12 13:08 */ namespace Application; use Bluz\Proxy\Layout; return function () use($bootstrap, $view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::breadCrumbs([$view->ahref('Test', ['test', 'index']), 'Bootstrap']); $view->result = $bootstrap(2); };
<?php /** * Grid of pages * * @author Anton Shevchuk * @created 27.08.12 10:08 */ namespace Application; use Bluz\Proxy\Layout; return function () use($view, $module, $controller) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::setTemplate('dashboard.phtml'); Layout::breadCrumbs([$view->ahref('Dashboard', ['dashboard', 'index']), __('Pages')]); $grid = new Pages\Grid(); $grid->setModule($module); $grid->setController($controller); $view->grid = $grid; };
<?php /** * Example of DB Query builder usage * * @author Anton Shevchuk * @created 07.06.13 18:28 */ namespace Application; use Bluz\Proxy\Layout; return function () use($view) { /** * @var Bootstrap $this * @var \closure $bootstrap * @var \Bluz\View\View $view */ Layout::breadCrumbs([$view->ahref('Test', ['test', 'index']), 'DB Query Builders']); // all examples inside view };
<?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\View\Helper; use Bluz\Application\Application; use Bluz\Proxy\Layout; return function ($script = null) { if (Application::getInstance()->hasLayout()) { return Layout::headScript($script); } else { // it's just alias to script() call return $this->script($script); } };
<?php /** * Test of test of test * * @author Anton Shevchuk * @created 21.08.12 12:39 */ namespace Application; use Bluz\Proxy\Layout; return function () { /** * @var Bootstrap $this */ Layout::title('Test Module'); Layout::title('Append', Layout::POS_APPEND); Layout::title('Prepend', Layout::POS_PREPEND); Layout::breadCrumbs(['Test']); };
<?php /** * Example of forms handle * * @category Application * * @author dark * @created 13.12.13 18:12 */ namespace Application; use Bluz\Proxy\Layout; use Bluz\Proxy\Request; return function ($int, $string, $array, $optional = 0) use($view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::breadCrumbs([$view->ahref('Test', ['test', 'index']), 'Form Example']); if (Request::isPost()) { ob_start(); var_dump($int, $string, $array, $optional); $view->inside = ob_get_contents(); ob_end_clean(); $view->params = Request::getAllParams(); } };
* Create of CRUD * * @category Application * * @author dark * @created 14.05.13 10:50 */ namespace Application; use Bluz\Proxy\Layout; use Bluz\Proxy\Request; use Bluz\Validator\Exception\ValidatorException; return function () use($view) { /** * @var Bootstrap $this */ Layout::setTemplate('small.phtml'); $row = new Test\Row(); $view->row = $row; if (Request::isPost()) { $crud = Test\Crud::getInstance(); try { $crud->createOne(Request::getPost()); } catch (ValidatorException $e) { $row = $crud->readOne(null); $row->setFromArray(Request::getPost()); $result = ['row' => $row, 'errors' => $e->getErrors(), 'method' => Request::METHOD_POST]; } // TODO: example without AJAX calls } };
* * @author dark * @created 08.07.11 13:23 */ namespace Application; use Bluz\Proxy\Cache; use Bluz\Proxy\Layout; return function ($id = null) use($bootstrap, $view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::breadCrumbs([$view->ahref('Test', ['test', 'index']), 'Cache Data']); /* @var Bootstrap $this */ Layout::title('Check cache'); // try to load profile of current user if (!$id && $this->user()) { $id = $this->user()->id; } if (!$id) { throw new \Exception('User not found', 404); } /** * @var Users\Row $userRow */ if (!($userRow = Cache::get('user:'******'user:' . $id, $userRow, 30); } if (!$userRow) {
*/ namespace Application; use Bluz\Application\Exception\NotFoundException; use Bluz\Proxy\HttpCacheControl; use Bluz\Proxy\Layout; use Bluz\View\View; return function ($alias) use($view) { /** * @var Bootstrap $this * @var View $view * @var Pages\Row $page */ $page = Pages\Table::getInstance()->getByAlias($alias); if (!$page) { // throw NOT FOUND exception // all logic of error scenario you can found in default error controller // see /application/modules/error/controllers/index.php throw new NotFoundException(); } else { // setup HTML layout data Layout::title(esc($page->title), View::POS_PREPEND); Layout::meta('keywords', esc($page->keywords)); Layout::meta('description', esc($page->description)); // setup HTTP cache HttpCacheControl::setPublic(); HttpCacheControl::setLastModified($page->updated); // assign page to view $view->page = $page; } };
<?php /** * Example of grid * * @author Anton Shevchuk * @created 27.08.12 10:08 */ namespace Application; use Application\Test; use Bluz\Proxy\Layout; return function ($alias) use($view, $module, $controller) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::breadCrumbs([$view->ahref('Test', ['test', 'index']), 'Grid with Select']); $grid = new Test\SelectGrid(); $grid->setModule($module); $grid->setController($controller); $grid->setParams(['alias' => $alias]); $view->grid = $grid; return 'grid-sql.phtml'; };
<?php /** * Default dashboard module/controller * * @author Anton Shevchuk * @created 06.07.11 18:39 * @return \Closure */ /** * @namespace */ namespace Application; use Bluz\Proxy\Layout; return function () { /** * @var Bootstrap $this */ Layout::breadCrumbs([__('Dashboard')]); Layout::setTemplate('dashboard.phtml'); };
<?php /** * Route examples * * @author Anton Shevchuk * @created 12.06.12 13:08 */ namespace Application; use Bluz\Proxy\Layout; return function () use($view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::breadCrumbs([$view->ahref('Test', ['test', 'index']), 'Routers Examples']); };
<?php /** * Default module/controllers * * @author Anton Shevchuk * @created 06.07.11 18:39 * @return closure */ namespace Application; use Bluz\Proxy\Layout; use Bluz\Proxy\Session; return function () use($view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::breadCrumbs([$view->ahref('Test', ['test', 'index']), 'Session']); Layout::title("Test/Index"); Session::set('test', Session::get('test') ?: 'Session time: ' . date("H:i:s")); $view->title = Layout::title(); $view->session = Session::get('test'); // if ($identity = $app->user()) { // var_dump($acl->isAllowed('index/index', $identity['sid'])); // var_dump($acl->isAllowed('index/test', $identity['sid'])); // var_dump($acl->isAllowed('index/error', $identity['sid'])); // } else { // Auth::authenticate('admin', '123456'); // } };
<?php /** * Grid of users * * @author Anton Shevchuk * @created 02.08.12 18:39 * @return closure */ namespace Application; use Application\Users; use Bluz\Controller\Controller; use Bluz\Proxy\Db; use Bluz\Proxy\Layout; /** * @privilege Management * @return \closure */ return function () { /** * @var Controller $this */ Layout::setTemplate('dashboard.phtml'); Layout::breadCrumbs([Layout::ahref('Dashboard', ['dashboard', 'index']), __('Users')]); $grid = new Users\Grid(); $grid->setModule($this->module); $grid->setController($this->controller); $this->assign('roles', Db::fetchAll('SELECT * FROM acl_roles')); $this->assign('grid', $grid); };
* @category Application * * @author Anton Shevchuk * @created 14.11.13 10:45 */ namespace Application; use Bluz\Db\Relations; use Bluz\Proxy\Db; use Bluz\Proxy\Layout; return function () use($view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::breadCrumbs([$view->ahref('Test', ['test', 'index']), 'DB Relations']); /* @var Pages\Row */ $page = Pages\Table::findRow(5); $user = $page->getRelation('Users'); echo "<h2>Page Owner</h2>"; var_dump($user); $pages = $user->getRelations('Pages'); echo "<h2>User pages</h2>"; var_dump(sizeof($pages)); $roles = $user->getRelations('Roles'); echo "<h2>User roles</h2>"; var_dump($roles); echo "<h2>User with all relations</h2>"; var_dump($user); $result = Db::fetchRelations("SELECT '__users', u.*, '__pages', p.* FROM users u LEFT JOIN pages p ON p.userId = u.id"); echo "<h2>User - Page relation</h2>";
break; case 501: $title = __("Not Implemented"); $description = __("The server does not understand or does not support the HTTP method"); break; case 503: $title = __("Service Unavailable"); $description = __("The server is currently unable to handle the request due to a temporary overloading"); Response::setHeader('Retry-After', '600'); break; default: $title = __("Internal Server Error"); $description = __("An unexpected error occurred with your request. Please try again later"); break; } // check CLI or HTTP request if (Request::isHttp()) { // simple AJAX call, accept JSON if (Request::getAccept(['application/json'])) { $this->useJson(); Messages::addError($description); return null; } // dialog AJAX call, accept HTML if (!Request::isXmlHttpRequest()) { $this->useLayout('small.phtml'); } } Layout::title($title); return ['error' => $title, 'description' => $description]; };
<?php /** * Demo of View helpers * * @category Application * * @author dark * @created 14.05.13 16:12 */ namespace Application; use Bluz\Proxy\Layout; use Bluz\Proxy\Request; return function ($sex = false, $car = 'none', $remember = false) use($view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::breadCrumbs([$view->ahref('Test', ['test', 'index']), 'View Form Helpers']); /** * @var Bootstrap $this */ $view->sex = $sex; $view->car = $car; $view->remember = $remember; if (Request::isPost()) { $view->params = Request::getAllParams(); } };