[Prestashop development] Lesson 2: How to create a New page, New controller in Prestashop website?
One page ( it can be called as a controller) in Prestashop website is a properly page, you can directly view it from a URL like Product page, Category page, CMS page…
In this post, we will take to help you how to create a new page, a new controller in Frontend (website) of Prestashop system.
Step 1: Create and Install a your prestashop custom module based on this post: Lesson 1: How to create a New module for Prestashop?
In this post, our prestashop custom module is buyaddonsexam module.
Step 2: Create a template *.tpl file for new page (controller)
1.Create a new directory: modules\your_module_name\views\templates\front
2.If you want use this controller in Prestashop 1.6, Create a template file: modules\your_module_name\views\templates\front\demo_template16.tpl with the content that contains any HTML, Smarty code, example:
<h2>Hello</h2>
<p> This is my New page</p>Something…
3.If you want use this controller in Pretashop 1.7, Create a template file: modules\your_module_name\views\templates\front\demo_template17.tpl with the content that contains any HTML, Smarty code:
{extends file=’page.tpl’}
{block name=’page_content’}
Your HTML, Smarty Code…
{/block}
Note: A template file (*.tpl) for Prestashop 1.7 required 2 lines:
{extends file=’page.tpl’}
{block name=’page_content’} …
You must write your HTML, Smarty code to output your content between {block name=’page_content’} and {/block}
Step 3: Create a PHP Controller file for new page. In my example, my controller is named “demo”.
1.Create a new PHP file in modules\your_module\controllers\front\your_controller.php
Example my file: modules\buyaddonsexam\controllers\front\demo.php
2. Declare a PHP Class is named Your_moduleYour_controllerModuleFrontController contains initContent() method like sample file:
class BuyaddonsExamDemoModuleFrontController extends ModuleFrontController
{public function initContent(){if (Tools::version_compare(_PS_VERSION_, ‘1.7’, ‘>=’)) {$this ->setTemplate(‘module:buyaddonsexam/views/templates/front/demo_template17.tpl’);} else {$this->setTemplate(‘demo_template16.tpl’);}parent::initContent();}}
Change this PHP code to your controller:
- BuyaddonsExamDemoModuleFrontController to your Class name: [Your_module][Your controller]ModuleFrontController
- module:buyaddonsexam/views/templates/front/demo_template17.tpl to your template file for Prestashop 1.7 with formatted path: module:[Your_module]/views/templates/front/[your_template_file].tpl
Step 5: View your page in browser.
Go to your prestashop website, enter URL: http://your-prestashop-store/index.php?controller=[Your_controller]&fc=module&module=[Your_module]
My example: http://your-prestashop-store/index.php?controller=demo&fc=module&module=buyaddonsexam
You can view your a new page like this in Prestashop 1.7
In Prestashop 1.6, the page output:
How to output variables from PHP file into page?
Step 1: In your PHP controller file, you have some variables. if you want display them in a prestashop page, you must assign there variables to template file (*.tpl) with the function: $this->context->smarty->assign(“name”, $value);
Step 2: Open a template file, example views\templates\front\demo_template17.tpl file, print it with statement:
{$store_url}
{$company_name}
My demo_template17.tpl file
There variables are displayed in new page:
You can read more some our other posts for develop extend controller for your prestashop store:
- Print Links, generate URLs from a template, *.tpl file in Prestashop 1.7 with {url}?
- How to create a New module for Prestashop?
Download our sample module
Download FULL buyaddonsexam.zip module for this post at here: buyaddonsexam_new_page.zip
Leave a reply
You must be logged in to post a comment.