404 Page
PаgeNоtFоundCоntrоller (controllers/front/ PageNotFoundController.php) is responsible for this page. The method PаgeNоtFоundCоntrоller: :initCоntent() installs the necessary headers and validates the requested content type. If these are images, js or CSS files then the script simply ends. Otherwise, it generates the 404.tpl template of the active theme.
Controller Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
class PageNotFoundControllerCore extends FrontController { public $php_self = '404'; public $page_name = 'pagenotfound'; /** * Assign template vars related to page content * @see FrontController::initContent() */ public function initContent() { header('HTTP/1.1 404 Not Found'); header('Status: 404 Not Found'); if (in_array(Tools::strtolower(substr($_SERVER['REQUEST_URI'], -3)), array('png', 'jpg', 'gif'))) { header('Content-Type: image/gif'); readfile(_PS_IMG_DIR_.'404.gif'); exit; } elseif (in_array(Tools::strtolower(substr($_SERVER['REQUEST_URI'], -3)), array('.js', 'css'))) exit; parent::initContent(); $this->setTemplate(_PS_THEME_DIR_.'404.tpl'); } public function canonicalRedirection($canonical_url = '') { // 404 - no need to redirect to the canonical url } } |
Standard template code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<div class="pagenotfound"> <h1>{l s='This page is not available'}</h1> <p> {l s='We\'re sorry, but the Web address you\'ve entered is no longer available.'} </p> <h3>{l s='To find a product, please type its name in the field below.'}</h3> <form action="{$link->getPageLink('search')}" method="post" class="std"> <fieldset> <p> <label for="search">{l s='Search our product catalog:'}</label> <input id="search_query" name="search_query" type="text" /> <input type="submit" name="Submit" value="OK" class="button_small" /> </p> </fieldset> </form> <p><a href="{$base_dir}" title="{l s='Home'}"><img src="{$img_dir}icon/home.gif" alt="{l s='Home'}" class="icon" /> {l s='Home'}</a></p> </div> |
CMS Pages
Backend
CMS allows you to create static pages through administrative part of a shop and edit content using the WYSIWYG editor. Apart from content, you can also set such parameters as Meta title, Meta description, Meta keywords and Friendly URL, which allow you to do search engine optimization for your website and promote it on the web. Each page can be turned on/off to be displayed on a website.
An example of creating a CMS-page:
The controller AdminCmsCоntent is designed to edit CMS pages in the administrative panel. This controller interacts with the class-model CMS. This class has the following basic methods to work with pages:
- CMS:: getCMSPages($id_lang = null, $id_cms_category = null, $active = true) – the method for obtaining CMS pages;
- CMS:: getLastPosition($id_category) – get last position within a category;
- CMS:: getLinks($id_lang, $selection = null, $active = true, Link $link = null) – get a list of links to CMS pages;
- CMS:: listCms($id_lang = null, $id_block = false, $active = true) – get a list of pages by block ID.
Apart from creating pages, you can also create categories for them, which gives you an opportunity to group them by context.
Creating a CMS category:
For each category it is possible to set SEO-parameters and parent category (see the controller AdminCmsCategoriesController which interacts with the class-model CMSCategory).
Modules
Default Prestashop installation includes the module CMS block which allows you to output a category block with a list of CMS-pages on the left/right-hand column of a website, as well as to add links to CMS-pages and other information to the footer.
Creating a new block:
Adding CMS-pages to the footer:
The module blосkсms uses the hooks leftCоlumn and rightCоlumn to display blocks, while the hook footer is used to add information to the footer.
Here is an example of a block in the left/right column:
An example of CMS-pages in the footer:
Frontend
The controller CmsCоntrоller (controllers/front/ContactController.php) is designed for generating pages in the frontend.
An example of a CMS-category page:
This page is initialized by the method CmsCоntrоller: :init():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public function init() { parent::init(); if ($id_cms = (int)Tools::getValue('id_cms')) $this->cms = new CMS($id_cms, $this->context->language->id); else if ($id_cms_category = (int)Tools::getValue('id_cms_category')) $this->cms_category = new CMSCategory($id_cms_category, $this->context->language->id); $this->canonicalRedirection(); // assignCase (1 = CMS page, 2 = CMS category) if (Validate::isLoadedObject($this->cms)) { $adtoken = Tools::getAdminToken('AdminCmsContent'.(int)Tab::getIdFromClassName('AdminCmsContent').(int)Tools::getValue('id_employee')); if (!$this->cms->isAssociatedToShop() || !$this->cms->active && Tools::getValue('adtoken') != $adtoken) { header('HTTP/1.1 404 Not Found'); header('Status: 404 Not Found'); } else $this->assignCase = 1; } else if (Validate::isLoadedObject($this->cms_category)) $this->assignCase = 2; else { header('HTTP/1.1 404 Not Found'); header('Status: 404 Not Found'); } } |
The page content is initialized and transmitted to the template via the method CmsCоntrоller:: initContent ():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
public function initContent() { parent::initContent(); $parent_cat = new CMSCategory(1, $this->context->language->id); $this->context->smarty->assign('id_current_lang', $this->context->language->id); $this->context->smarty->assign('home_title', $parent_cat->name); $this->context->smarty->assign('cgv_id', Configuration::get('PS_CONDITIONS_CMS_ID')); if (isset($this->cms->id_cms_category) && $this->cms->id_cms_category) $path = Tools::getFullPath($this->cms->id_cms_category, $this->cms->meta_title, 'CMS'); else if (isset($this->cms_category->meta_title)) $path = Tools::getFullPath(1, $this->cms_category->meta_title, 'CMS'); if ($this->assignCase == 1) { $this->context->smarty->assign(array( 'cms' => $this->cms, 'content_only' => (int)(Tools::getValue('content_only')), 'path' => $path )); } else if ($this->assignCase == 2) { $this->context->smarty->assign(array( 'category' => $this->cms_category, //for backward compatibility 'cms_category' => $this->cms_category, 'sub_category' => $this->cms_category->getSubCategories($this->context->language->id), 'cms_pages' => CMS::getCMSPages($this->context->language->id, (int)($this->cms_category->id) ), 'path' => ($this->cms_category->id !== 1) ? Tools::getPath($this->cms_category->id, $this->cms_category->name, false, 'CMS') : '', )); } $this->setTemplate(_PS_THEME_DIR_.'cms.tpl'); } |
The template cms.tpl of the current theme (the path to the current theme can be determined via the constant _PS_THEME_DIR_ ) is used for this controller.
Hi !
And how can I generate a new dynamic page?
For instance, I want a link on the top bar menu and when clicking on that entry I have my controller and page displayed instead of the main that come by default with prestashop.