Jump to content

Create page programmatically


Chill_user

Recommended Posts

Hello to all,

I have a question about how to create custom page programmatically.

I need to create a page, where I can place my content.

I know that I can create controller, than go to the "Traffic and SEO" and add a custom controller. It's like semi-programmatical method.

But my question is how to create the page with PHP

Thanks a lot!

P.S. I know it's not easy, but you can explain just logic.

P.S2 I know that we have CMS class in /classes but it creates a page where you need to define a controller

Link to comment
Share on other sites

Tutorial for Prestashop 1.7

1. create front controller

<?php

class custompageControllerCore extends FrontController
{
    public $php_self = 'custompage';

    public function initContent()
    {
        $this->context->smarty->assign(
            array(
                'variable1' => 'Variable 1',
                'variable2' => 'Variable 2',
            )
        );

    parent::initContent();
      $this->setTemplate('custompage');
    }

}

2. save to ./controllers/front/custompageController.php

3. create tpl file


  {extends file='page.tpl'}
  
  {block name='page_title'}
    <span class="sitemap-title">{l s='Custom page' d='Shop.Theme'}</span>
  {/block}
  
  {block name='page_content'}
      <h2>My test page content</h2>
      <span>{$variable1}</span><br />
      <span>{$variable2}</span><br />
  {/block}

4. save to ./themes/your theme/templates/custompage.tpl

5. Admin -> Shop parameters -> Traffic & SEO

6. Click add new page

7. Select "Page name"  custompage

8. Rewritten URL = my-custom-page

9. Save

10. Admin -> Advanced Parameters -> Performance

11. clear cache and recompile template

12. go to: https://your-store-url/my-custom-page

13. go to this post and like by clicking on the gray heart below the post 😉

Link to comment
Share on other sites

Follow these instructions to programmatically create PHP and tpl files.
You also know where to store them.


Instead of creating SEO you can insert values into the database (`ps_meta_lang`).

$languages = Language::getLanguages(false, $this->context->shop->id);
        $get_meta_id = Db::getInstance()->getValue('SELECT id_meta FROM '._DB_PREFIX_.'meta_lang ORDER BY id_meta DESC;') + 1;
        
        foreach ($languages as $lang) {
		        Db::getInstance()->Execute(
                   "INSERT INTO `"._DB_PREFIX_."meta_lang` (`id_meta`, `id_shop`, `id_lang`, `title`, `description`, `keywords`, `url_rewrite`) 
                    VALUES (".$get_meta_id.", 1, ".$lang['id_lang'].", 'my custom page', 'created my custom page', 'my,custom,page', 'my-custom-page')"
                   );
        }

Db::getInstance()->Execute("INSERT INTO "._DB_PREFIX_."meta (`id_meta`, `page`, `configurable`) VALUES (".$get_meta_id.", 'custompage', 1)");

You can also add cache clear to your script.

Tools::clearSmartyCache();
Tools::clearXMLCache();
Media::clearCache();
Tools::generateIndex();

 

Edited by Guest (see edit history)
Link to comment
Share on other sites

  • 2 weeks later...

Hi,

@Guest

Custom controller works fine if extending file page...

But is there a way to do same just for tpl file?Idea is to create a custom controller, add some code/variables and push to custom tpl. Then later include that file in other tpl's in template?

Right now variables are shown only in that page/controller created. But if that tpl is included in different tpl it doesnt work...

Thanks

 

 

Link to comment
Share on other sites

Hi,

@Guest

It's not about being smart enough. It's more about sharing your experience with community and other developers. Sometimes you can solve issue within 5 min. just asking for help instead of doing research for 2 or more days.

Thanks

Link to comment
Share on other sites

  • 8 months later...
  • 11 months later...
On 4/21/2020 at 11:22 AM, Guest said:

Tutorial for Prestashop 1.7

1. create front controller

<?php

class custompageControllerCore extends FrontController
{
    public $php_self = 'custompage';

    public function initContent()
    {
        $this->context->smarty->assign(
            array(
                'variable1' => 'Variable 1',
                'variable2' => 'Variable 2',
            )
        );

    parent::initContent();
      $this->setTemplate('custompage');
    }

}

2. save to ./controllers/front/custompageController.php

3. create tpl file


  {extends file='page.tpl'}
  
  {block name='page_title'}
    <span class="sitemap-title">{l s='Custom page' d='Shop.Theme'}</span>
  {/block}
  
  {block name='page_content'}
      <h2>My test page content</h2>
      <span>{$variable1}</span><br />
      <span>{$variable2}</span><br />
  {/block}

4. save to ./themes/your theme/templates/custompage.tpl

5. Admin -> Shop parameters -> Traffic & SEO

6. Click add new page

7. Select "Page name"  custompage

8. Rewritten URL = my-custom-page

9. Save

10. Admin -> Advanced Parameters -> Performance

11. clear cache and recompile template

12. go to: https://your-store-url/my-custom-page

13. go to this post and like by clicking on the gray heart below the post 😉

 

Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...