lang | layout | updated_at | title | identifier | categories | published | order | ||
---|---|---|---|---|---|---|---|---|---|
en |
article_with_sidebar |
2014-12-18 00:00 |
Creating new page |
ref_0VgqyxB8 |
|
true |
100 |
This article describes how developers can create a new page in X-Cart. For instance, we want to create a page in admin area (admin.php?target=tony_custom
) that will show some specific information. This guide explains how to achieve this task.
- Introduction
- Table of Contents
- Before get started
- Creating page in admin area
- Creating page via macro
- Creating page in customer area
- Module pack
First thing to do is to {% link "create an empty module" ref_G2mlgckf %}. We are creating a module with developer ID Tony and module ID PageDemo.
For the sake of example, our task is to create the page which will be available at admin.php?target=tony_custom
address and will display Hello world! text.
-
Create new controller class. Since we want our page to be opened at
admin.php?target=tony_custom
, the controller class must be named TonyCustom. If you need more info about how controllers work in X-Cart, look at {% link "Controllers" ref_hkVaxgds %} article. -
We create the
classes/XLite/Module/Tony/PageDemo/Controller/Admin/TonyCustom.php
file with the following content:<?php namespace XLite\Module\Tony\PageDemo\Controller\Admin; class TonyCustom extends \XLite\Controller\Admin\AAdmin { }
As you can see, it is pretty empty, but since no data should be processed from the request, we do not need any extra methods here.
-
Create new viewer class that will manage the data output. This viewer class should sit in the
classes/XLite/Module/Tony/PageDemo/View/Page/Admin/
directory and have the same as controller class. This is just an agreement and all page viewer classes follow the same principle. We are creating theclasses/XLite/Module/Tony/PageDemo/View/Page/Admin/TonyCustom.php
file with the following content:5.2.x and earlier 5.3.x```php namespace XLite\Module\Tony\PageDemo\View\Page\Admin;/**
- @ListChild (list="admin.center", zone="admin") */
class TonyCustom extends \XLite\View\AView { public static function getAllowedTargets() { return array_merge(parent::getAllowedTargets(), array('tony_custom')); }
protected function getDefaultTemplate() { return 'modules/Tony/PageDemo/page/tony_custom/body.tpl'; }
}
</div> <div data-tab="tab_1-2" class="ui bottom attached active tab segment" markdown="1"> ```php <?php namespace XLite\Module\Tony\PageDemo\View\Page\Admin; /** * @ListChild (list="admin.center", zone="admin") */ class TonyCustom extends \XLite\View\AView { public static function getAllowedTargets() { return array_merge(parent::getAllowedTargets(), array('tony_custom')); } protected function getDefaultTemplate() { return 'modules/Tony/PageDemo/page/tony_custom/body.twig'; } }
-
Let us walk through each line of this code.
namespace XLite\Module\Tony\PageDemo\View\Page\Admin;
This is just a namespace definition.
/** * @ListChild (list="admin.center", zone="admin") */
This part is very important. It registers this viewer class to be displayed in the central area of admin area.
class TonyCustom extends \XLite\View\AView
Just a class definition.
public static function getAllowedTargets() { return array_merge(parent::getAllowedTargets(), array('tony_custom')); }
The
getAllowedTargets()
method defines which targets will trigger this viewer class. The current implementation means that, if target=tony_custom, then this viewer class will display its content in the central area. If there is any other target, then this viewer class will not be even run.protected function getDefaultTemplate() { return 'modules/Tony/PageDemo/page/tony_custom/body.twig'; }
The
getDefaultTemplate()
method defines what template will be used in order to output the content. The template path can be whatever you prefer. -
Now it is time to create the template defined in the
getDefaultTemplate()
method, so it would display Hello world! text. We are creating theskins/admin/modules/Tony/PageDemo/page/tony_custom/body.twig
(skins/admin/en/modules/Tony/PageDemo/page/tony_custom/body.tpl
for 5.2.x and earlier) file with the following content:Hello world!
-
We are done with this mod. Now it is time to re-deploy the store and check the results. If you open the following URL in your store:
admin.php?target=tony_custom
, you will see the following result:
You can {% link "create a page via macro" ref_HvrXVNvJ#X-CartSDK-Creatingpage %}, so it will save your time. In this case, all files will be created automatically and you will only have to go to the template file and define its content.
Imagine, we have a similar task of creating page (cart.php?target=tony_custom
) with Hello world! text, but in customer area. The process would be quite the same as for creating page for admin zone.
-
We create new controller class:
classes/XLite/Module/Tony/PageDemo/Controller/Customer/TonyCustom.php
. Notice that we changed the sub-directory from 'Controller/Admin/TonyCustom.php' to 'Controller/Customer/TonyCustom.php', this way X-Cart will understand that this controller for customer zone, not admin. -
The content of the controller class will be as follows:
<?php namespace XLite\Module\Tony\PageDemo\Controller\Customer; class TonyCustom extends \XLite\Controller\Customer\ACustomer { }
The implementation of the controller class is similar to admin's one, but it has different namespace (
XLite\Module\Tony\PageDemo\Controller\Customer
) and it extends different class (\XLite\Controller\**Customer\ACustomer
) -
We create new viewer class:
classes/XLite/Module/Tony/PageDemo/View/Page/Customer/TonyCustom.php
(again, notice change fromPage/Admin/TonyCustom.php
toPage/Customer/TonyCustom.php
in the path) with the following content:5.2.x and earlier 5.3.x```php namespace XLite\Module\Tony\PageDemo\View\Page\Customer;/**
-
@ListChild (list="center") */ class TonyCustom extends \XLite\View\AView { public static function getAllowedTargets() { return array_merge(parent::getAllowedTargets(), array('tony_custom')); }
protected function getDefaultTemplate() { return 'modules/Tony/PageDemo/page/tony_custom/body.tpl'; } }
</div> <div data-tab="tab_3-2" class="ui bottom attached active tab segment" markdown="1"> ```php <?php namespace XLite\Module\Tony\PageDemo\View\Page\Customer; /** * @ListChild (list="center") */ class TonyCustom extends \XLite\View\AView { public static function getAllowedTargets() { return array_merge(parent::getAllowedTargets(), array('tony_custom')); } protected function getDefaultTemplate() { return 'modules/Tony/PageDemo/page/tony_custom/body.twig'; } }
-
-
As you can see this implementation has only few differences:
namespace XLite\Module\Tony\PageDemo\View\Page\Customer;
namespace is a bit different;
/** * @ListChild (list="center") */
We use this
@ListChild
directive in order to insert this viewer class into central area of customer area, instead of admin one;5.2.x and earlier 5.3.x```php protected function getDefaultTemplate() { return 'modules/Tony/PageDemo/page/tony_custom/body.tpl'; } ``````php protected function getDefaultTemplate() { return 'modules/Tony/PageDemo/page/tony_custom/body.twig'; } ```The template for this viewer sits in other location. Aside from that, the implementation is the same.
-
Finally, we need to create the template mentioned in the
getDefaultTemplate()
method. We create theskins/customer/modules/Tony/PageDemo/page/tony_custom/body.twig
(skins/default/en/modules/Tony/PageDemo/page/tony_custom/body.tpl
for 5.2.x and earlier) template (notice, that we create this template in theskins/customer/
directory, instead ofskins/admin/
one – it is so, because this template will be displayed in customer store-front) with Hello world! content. -
Re-deploy the store and open the
cart.php?target=tony_custom
page after that. You will see the following result:
- For X-Cart 5.2.x and earlier: Tony-PageDemo-v5_1_0.tar
- For X-Cart 5.3.x and later: Tony-PageDemo-v5_3_0.tar