Saturday, February 28, 2015



          Create a Custom Page in OpenCart

OpenCart is assembled utilizing the prevalent programming MVC design. There is additionally one more component added to this example named "L" - a dialect part - so its called MVC-L example in OpenCart. I won't go into the subtle elements of the MVC design as its an exceptionally well known and commonplace configuration example and we've secured it in incredible detail in different excercises.

That said, despite everything we have to take a gander at the work process utilizing this example.

To begin with, the controller goes about as an entrance point for any page in which you'll characterize the vast majority of the application rationale. The model manages the back-end database, and the perspective is in charge of setting up the substance to be rendered to the end client. In the connection of OpenCart, you'll at any rate need to actualize a controller and a perspective so as to make another custom page.


Setting Up the Controller
To start with, we should attempt to comprehend the part of the controller in OpenCart. The controller is the first component that will get executed when you ask for any page. Principally, its in charge of setting up the variables which will be later on utilized as a part of the perspective for the showcase. Albeit in the non specific controller, there are a ton of things occurrence:

·         You can stack dialect documents so you can utilize dialect variables for the static content presentation.

·         You can load model documents so you can utilize techniques characterized as a part of those models to bring information from the back-end database.

·         You can characterize the layout document which will be utilized by the perspective.

·         You can set up the custom variables by allocating them content which will be utilized as a part of the layout record.

·         You can announce the kids formats which you might want to show as a piece of the principle format. The easiest sample of this is the header and footer formats which you might want to show in your fundamental format.

·         At last, you can likewise set qualities for the stuff like archive title, meta depiction and so forth


Enough hypothesis, isn't that so? We should perceive how our custom controller looks like. Feel free to make another index custompage underneath catalog/controller. Make another record mycustompage.php underneath catalog/controller/custompage. Glue the accompanying code in the recently made controller document "mycustompage.php".
1.  <?php 
2.  class ControllerCustompageMycustompage extends Controller {
3.  public function index() {
4.  // set title of the page
5.  $this->document->setTitle("My Custom Page");
6.  // define template file
7.  if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/custompage/mycustompage.tpl')) {
8.  $this->template = $this->config->get('config_template') . '/template/custompage/mycustompage.tpl';
9.  } else {
10.             $this->template = 'default/template/custompage/mycustompage.tpl';
11.             }
12.             // define children templates
13.             $this->children = array(
14.             'common/column_left',
15.             'common/column_right',
16.             'common/content_top',
17.             'common/content_bottom',
18.             'common/footer',
19.             'common/header'
20.             );
21.             // set data to the variable
22.             $this->data['my_custom_text'] = "This is my custom              page.";
23.               // call the "View" to render the output
24.               $this->response->setOutput($this->render());
25.               }
26.               }
27.               ?>



We should comprehend the naming tradition of the controller class. The controller class name is built by taking after the registry structure and camel case tradition. Notice that the class name starts with "Controller" magic word took after by the index name ("Custompage") in which the class record dwells. Lastly the name of class record ("Mycustompage") is attached toward the end. Presently we should analyze every segment in point of interest.

First and foremost we've set the estimation of the html title tag for our custom page.


1.   $this->document->setTitle("My Custom Page");



In the following area, we've characterized the layout record name which will be utilized by the "View" component. An imperative thing to note here is that we have initially watched that if the layout document is accessible in the custom subject set from the back-end, in the event that its accessible in the custom topic we'll utilize that else we'll utilize the format record as a part of the "default" subject.

This is the concept of template overriding.

if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/custompage/mycustompage.tpl')) {
  $this->template = $this->config->get('config_template') . '/template/custompage/mycustompage.tpl';
} else {
  $this->template = 'default/template/custompage/mycustompage.tpl';
}


We have likewise characterized the kids formats utilizing the show. As a sample, common/header maps to the layout document spotted at catalog/view/theme/default/template/common/header.tpl
and executes the same. Consequence of that will be doled out to $header variable which you can use in your format record to show the site header.

$this->children = array(
  'common/column_left',
  'common/column_right',
  'common/content_top',
  'common/content_bottom',
  'common/footer',
  'common/header'
          );


Besides, we've showed how you can set up the custom variables which will be accessible in the format record. Despite the fact that we have utilized a basic static content here, you could appoint a more sensible substance, say for instance an items exhibit brought from the database.
1.   $this->data['my_custom_text'] = "This is my custom page.";

So's the look of controller work process.

In the front-end, you'll get to this controller by utilizing the inquiry string variable  route=custompage/mycustompage. It's critical to note here is that in the event that you characterize the controller strategy with whatever other name except index you need to specify that as well in the URL

Case in point, in the event that you have made strategy named custom, your front-end URL arrangement ought to look like route=custompage/mycustompage/custom.

We should see how OpenCart maps any url to the particular controller document. The arrangement of the course variable is {directory}/{filename}/{methodname}. {directory}maps to the index catalog/controller. {filename}maps to the name of the controller document under catalog/controller/{directory}. Lastly, it'll search for the controller technique named {methodname}in the event that its detailed in the course, else it'll call the default index method.






Prepare the View

In this section, we'll create the view template file which we defined earlier in the controller index method. Go ahead and create a new directory custompage underneath catalog/view/theme/default/template. Create a new file mycustompage.tpl underneath catalog/view/theme/default/template/custompage. Paste the following content in the newly created template file mycustompage.tpl.
<?php
    echo $header;
    echo $column_left;
    echo $column_right; ?>
    <div id="content">
        <?php
            echo $content_top;
            echo $my_custom_text;
            echo $content_bottom;
        ?>
    </div>
<?php echo $footer; ?>
So this is our main layout template file which is responsible for displaying the content of our custom page. In this template file, we have just used the variables we've set up in the controller's index method. 
The only custom variable in this template file is $my_custom_text, rest of the variables contain the contents related to the children templates like header, footer etc. The variables $column_left, $column_right, $content_top and $content_bottom are used to display the modules assigned to our custom page from the back-end.
If you want to assign modules to our custom page, first you need to create a new layout entry from the back-end. After you have added a new layout, you would like to add a route such as custompage/mycustompage entry for that layout. Now you can assign any module to our custom page as well.