[Prestashop development] Lesson 3: How to create a New page (controller) in Prestashop Admin?

[Prestashop development] Lesson 3: How to create a New page (controller) in Prestashop Admin?

One admin page ( it can be called as a controller) in Prestashop Backoffice is a properly page, you can directly view it from a URL like Catalog page, Products Manager, Orders Manager… You can view all admin pages of Prestashop in database from 2 tables: ps1_tab, ps1_tab_lang.

In this post, we will take to help you how to create a new page, a new controller in admin 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 admin page (admin controller)

1.Create a new directory: modules\your_module_name\views\templates\admin

2.Create a template filemodules\your_module_name\views\templates\admin\template2019.tpl with the content that contains any HTML, Smarty code, example:

<h2>This is a new admin page</h2>
<p>Hello buy-addons.com Team! </p>

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\admin\your_controller.php

Example my file: modules\buyaddonsexam\controllers\admin\demo.php

2. Declare a PHP Class is named [Your_controller]Controller contains initContent() method like sample file:

preatashop-admin-controller-2

Download our demo.php file at here:  admin_demo.php.txt

Change this PHP code to your controller:

Explanation for code in this admin controller:

  • Compile and output template file for admin controller, you can change buyaddonsexam/views/templates/admin/template2019.tpl to your path_to_template_file

$template_file = _PS_MODULE_DIR_. ‘buyaddonsexam/views/templates/admin/template2019.tpl’;
$content = $this->context->smarty->fetch($template_file);

  • display a template to admin page

$this->context->smarty->assign(array(
‘content’ => $content,
));

Step 4: Register a new menu item for this controller in admin area.

Open a PHP file: modules\your_module\your_module.php

Example: modules\buyaddonsexam\buyaddonsexam.php

Go to Install() method and add this code for create a new menu item in admin area.

/////////////////
$tab = new Tab();
$tab->active = 1;
$tab->class_name = ‘Demo’;
$tab->position = 3;
$tab->name = array();
foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang[‘id_lang’]] = ‘Demo Page’;
}
$tab->id_parent = (int) Tab::getIdFromClassName(‘SELL’);
$tab->module = $this->name;
$tab->add();
$tab->save();
/////////////////////////

  • $tab = new Tab(): create a menu item
  • $tab->active = 1: Active this menu in admin area
  • $tab->class_name = ‘Demo’: The Name of Controller (created it in step 3)
  • $tab->name: Menu text is displayed in Admin
  • $tab->id_parent: ID of parent menu, you can find ID in ps_tab from database.
  • $tab->module: A module contain this admin controller
  • $tab->add(), $tab->save(): Insert this menu into database (table: ps_tab, ps_tab_lang)

preatashop-admin-controller-5

Reset your prestashop module, you can see a new menu item in left sidebar:

preatashop-admin-controller-4

Step 5: View your page in browser.

Go to your prestashop website, enter URL: http://your-prestashop-store/ADMIN_DIR/index.php?controller=[Your_controller]

My example: http://your-prestashop-store/admin321/index.php?controller=demo

You can view your a new page like this in Prestashop 1.7

preatashop-admin-controller-6

 

How to output variables from PHP file into page?

Step 1: In your PHP admin 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);

preatashop-admin-controller-7

Step 2: Open a template file, example views\templates\front\template2019.tpl file, print it with statement:

 {$store_url}

{$company_name}

My template2019.tpl file

preatashop-admin-controller-8

There variables are displayed in new page:

preatashop-admin-controller-9

You can read more some our other posts for develop extend controller for your prestashop store:

Download our sample module

Download FULL buyaddonsexam.zip module for this post at here:  buyaddonsexam_admin_controller.zip

Related Articles

Leave a reply

You must be logged in to post a comment.