Jump to content

Custom module returns status 500


Recommended Posts

Hi

I have a problem with a custom module. It is such a training before the module I am working on

I would like the module to work on both user and admin side.

On the admin side I want a new tab to appear in the left panel. The item itself appears only in the wrong place. It works correctly because it displays the item I want.

The problem appears on the user side: The template link itself on the "My Account" page appears, but the redirect returns:
```
http://localhost:8080/pl/test
```
and it returns this status 500 (image error500.png)

files:

testmodule/controllers/front/TestController.php:

<?php

class TestModuleTestController extends ModuleFrontController
{

    public function __construct() {
        parent::__construct();
    }

    public function init()
    {
        $this->page_name = 'myfunction';
        $this->display_column_left = false;
        $this->display_column_right = false;
        parent::init();
    }

    public function initContent()
    {
        parent::initContent();

        $this->context->smarty->assign([
            'message' => 'TEST'
        ]);

//        $this->setTemplate('module:testmodule/views/templates/front/test.tpl');
        $this->setTemplate(__FILE__, 'views/templates/front/tpl_name.tpl');
    }
}

 

testmodule/controllers/admin/AdminTestController.php:

<?php

class AdminTestController extends ModuleAdminController
{
    public function __construct()
    {
        parent::__construct();
        $this->bootstrap = true;
    }

    public function initContent()
    {
        parent::initContent();

        // Przypisanie szablonu z napisem "TEST"
        $this->context->smarty->assign([
            'content' => 'TEST'
        ]);

        $this->setTemplate('test.tpl');
    }
}

testmodule/views/templates/admin/test.tpl:

<div class="panel">
    <h1>{$content} X</h1>
</div>

testmodule/views/templates/front/test.tpl:

<h1>TEST</h1>

testmodule/views/templates/front/account-tab.tpl:

<li class="item">
    <a href="{$testPageUrl}" title="{l s='Test' mod='testmodule'}">
        {l s='Test Page' mod='testmodule'} X
    </a>
</li>

The display itself in the user panel works, the problem is only with the redirection to this view

 

Thank you in advance for your help

structure.png

error500.png

Link to comment
Share on other sites

@Nickz Thanks for your interest this topic

 

That's right. I am testing this on a DOCKER environment on windows 10. I used the official Docker-compose available from the prestashop documentation website. https://devdocs.prestashop-project.org/8/basics/installation/environments/docker/#complete-docker-composeyml-for-reference

Localhost serves as my test environment for a module I'm working on.

Link to comment
Share on other sites

Hello,

The URL is most likely not /pl/test. You should check Admin -> Shop Parameters -> Traffic & SEO -> Route to modules. The actual URL will be something like /pl/module/<module_name>/test

The front controller does not follow the naming for the class name as well: https://devdocs.prestashop-project.org/8/modules/concepts/controllers/front-controllers/ - it should be TestModuleFrontController

Also, you should never hardcode the URL for the controller. You should generate it using the methods exposed by PrestaShop:

$this->context->link->getModuleLink(<module_name>, <controller_name>);

The above one generates links for the front controllers.

Finally, for debugging that 500 error, you can do one of the following:

1. Check the apache/nginx error logs

2. If these do not show anything, it is most likely a syntax error. In this case, search how you can display errors in PHP - once you've done the necessary changes to php.ini, you will see the error directly displayed in the browser.

Link to comment
Share on other sites

Hi @Andrei H Thanks for your interest this topic

 

In the testmodule.php file, I changed the value in `getModuleLink()`.

class TestModule extends Module
{
    ...

    public function hookDisplayCustomerAccount()
    {
        $this->context->smarty->assign([
            'testPageUrl' => $this->context->link->getModuleLink('testmodule', 'TestModuleFrontController')
        ]);
        return $this->display(__FILE__, 'views/templates/front/account-tab.tpl');
    }
   ...
}

 

I have corrected the name of the controller:

class TestModuleFrontController extends ModuleFrontController
{

    public function __construct() {
        parent::__construct();
    }

    public function init()
    {
        $this->page_name = 'myfunction';
        $this->display_column_left = false;
        $this->display_column_right = false;
        parent::init();
    }

    public function initContent()
    {
        parent::initContent();

        $this->context->smarty->assign([
            'message' => 'TEST'
        ]);

        $this->setTemplate('module:testmodule/views/templates/front/test.tpl');
    }
}

This made the status 500 no longer exist now. So technically the title problem is solved :) However, it redirects me to Page Not Found (the link changes after changing the language of the page, for example)

SEO & URLs has a '/test' page added.

 

Thank you in advance for your help

redirect.png

my_account.png

seoUrls.png

Link to comment
Share on other sites

@Nickz

I gave the URL as 'test', without any '/' or '.php'

 

@Andrei H

<?php

class TestModule extends Module
{
    ...

    public function hookDisplayCustomerAccount()
    {
        $this->context->smarty->assign([
            'testPageUrl' => $this->context->link->getModuleLink('testmodule', 'test')
        ]);
        return $this->display(__FILE__, 'views/templates/front/account-tab.tpl');
    }
    ...
}

I replaced 'TestModuleFrontController' -> 'test' however then again I get a 500 error from the browser 😕

500.png

my-account.png

seo.png

Link to comment
Share on other sites

Hello,

For debugging that 500 error, you can do one of the following:

1. Check the apache/nginx error logs

2. If these do not show anything, it is most likely a syntax error. In this case, search how you can display errors in PHP - once you've done the necessary changes to php.ini, you will see the error directly displayed in the browser.

Link to comment
Share on other sites

this is the part of logs from prestashop-container:

2024-08-20 22:28:18 
2024-08-20 22:28:18 * Checking if some-mysql is available...
2024-08-20 22:28:18 
2024-08-20 22:28:18 * DB server some-mysql is available, let's continue !
2024-08-20 22:28:18 
2024-08-20 22:28:18 * PrestaShop Core already installed...
2024-08-20 22:28:18 
2024-08-20 22:28:18 * Almost ! Starting web server now
2024-08-20 22:28:18 
2024-08-20 22:28:18 
2024-08-20 22:28:18 * No init script found, let's continue...
2024-08-20 22:34:34 172.18.0.1 - - [20/Aug/2024:20:34:33 +0000] "GET / HTTP/1.1" 302 231 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:39 172.18.0.1 - - [20/Aug/2024:20:34:34 +0000] "GET /pl/ HTTP/1.1" 200 16412 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:39 172.18.0.1 - - [20/Aug/2024:20:34:39 +0000] "GET /1-home_default/hummingbird-printed-t-shirt.jpg HTTP/1.1" 304 249 "http://localhost:8080/pl/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:39 172.18.0.1 - - [20/Aug/2024:20:34:39 +0000] "GET /21-home_default/brown-bear-printed-sweater.jpg HTTP/1.1" 304 250 "http://localhost:8080/pl/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:39 172.18.0.1 - - [20/Aug/2024:20:34:39 +0000] "GET /6-home_default/mug-the-best-is-yet-to-come.jpg HTTP/1.1" 304 249 "http://localhost:8080/pl/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:39 172.18.0.1 - - [20/Aug/2024:20:34:39 +0000] "GET /3-home_default/the-best-is-yet-to-come-framed-poster.jpg HTTP/1.1" 304 250 "http://localhost:8080/pl/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:39 172.18.0.1 - - [20/Aug/2024:20:34:39 +0000] "GET /4-home_default/the-adventure-begins-framed-poster.jpg HTTP/1.1" 304 250 "http://localhost:8080/pl/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:39 172.18.0.1 - - [20/Aug/2024:20:34:39 +0000] "GET /7-home_default/mug-the-adventure-begins.jpg HTTP/1.1" 304 250 "http://localhost:8080/pl/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:39 172.18.0.1 - - [20/Aug/2024:20:34:39 +0000] "GET /5-home_default/today-is-a-good-day-framed-poster.jpg HTTP/1.1" 304 250 "http://localhost:8080/pl/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:39 172.18.0.1 - - [20/Aug/2024:20:34:39 +0000] "GET /8-home_default/mug-today-is-a-good-day.jpg HTTP/1.1" 304 249 "http://localhost:8080/pl/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:39 172.18.0.1 - - [20/Aug/2024:20:34:39 +0000] "GET /pl/module/blockwishlist/action?action=getAllWishlist HTTP/1.1" 200 509 "http://localhost:8080/pl/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:39 172.18.0.1 - - [20/Aug/2024:20:34:39 +0000] "GET /pl/module/productcomments/CommentGrade?id_products%5B%5D=1&id_products%5B%5D=2&id_products%5B%5D=3&id_products%5B%5D=4&id_products%5B%5D=5&id_products%5B%5D=6&id_products%5B%5D=7&id_products%5B%5D=8&id_products%5B%5D=12&id_products%5B%5D=13&id_products%5B%5D=14&id_products%5B%5D=15&id_products%5B%5D=16&id_products%5B%5D=17&id_products%5B%5D=18&id_products%5B%5D=19 HTTP/1.1" 200 1209 "http://localhost:8080/pl/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:43 172.18.0.1 - - [20/Aug/2024:20:34:41 +0000] "GET /pl/moje-konto HTTP/1.1" 200 11860 "http://localhost:8080/pl/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:43 172.18.0.1 - - [20/Aug/2024:20:34:43 +0000] "GET /pl/module/blockwishlist/action?action=getAllWishlist HTTP/1.1" 200 509 "http://localhost:8080/pl/moje-konto" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:44 127.0.0.1 - - [20/Aug/2024:20:34:44 +0000] "OPTIONS * HTTP/1.0" 200 126 "-" "Apache/2.4.59 (Debian) (internal dummy connection)"
2024-08-20 22:34:45 127.0.0.1 - - [20/Aug/2024:20:34:45 +0000] "OPTIONS * HTTP/1.0" 200 126 "-" "Apache/2.4.59 (Debian) (internal dummy connection)"
2024-08-20 22:34:48 127.0.0.1 - - [20/Aug/2024:20:34:48 +0000] "OPTIONS * HTTP/1.0" 200 126 "-" "Apache/2.4.59 (Debian) (internal dummy connection)"
2024-08-20 22:34:56 172.18.0.1 - - [20/Aug/2024:20:34:55 +0000] "GET /pl/dane-osobiste HTTP/1.1" 200 13018 "http://localhost:8080/pl/moje-konto" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:34:56 172.18.0.1 - - [20/Aug/2024:20:34:56 +0000] "GET /pl/module/blockwishlist/action?action=getAllWishlist HTTP/1.1" 200 509 "http://localhost:8080/pl/dane-osobiste" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:35:00 172.18.0.1 - - [20/Aug/2024:20:35:00 +0000] "GET /pl/moje-konto HTTP/1.1" 200 11859 "http://localhost:8080/pl/dane-osobiste" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:35:00 172.18.0.1 - - [20/Aug/2024:20:35:00 +0000] "GET /pl/module/blockwishlist/action?action=getAllWishlist HTTP/1.1" 200 509 "http://localhost:8080/pl/moje-konto" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-20 22:35:03 172.18.0.1 - - [20/Aug/2024:20:35:02 +0000] "GET /pl/test HTTP/1.1" 500 185 "http://localhost:8080/pl/moje-konto" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-19 22:47:21 [Mon Aug 19 20:47:21.240429 2024] [mpm_prefork:notice] [pid 1] AH00170: caught SIGWINCH, shutting down gracefully
2024-08-20 22:28:18 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.3. Set the 'ServerName' directive globally to suppress this message
2024-08-20 22:28:18 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.3. Set the 'ServerName' directive globally to suppress this message
2024-08-20 22:28:18 [Tue Aug 20 20:28:18.340323 2024] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.59 (Debian) configured -- resuming normal operations
2024-08-20 22:28:18 [Tue Aug 20 20:28:18.340360 2024] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
2024-08-20 22:34:33 [Tue Aug 20 20:34:33.777865 2024] [php:warn] [pid 19] [client 172.18.0.1:55950] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2
2024-08-20 22:34:34 [Tue Aug 20 20:34:34.754022 2024] [php:warn] [pid 20] [client 172.18.0.1:55956] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2
2024-08-20 22:34:39 [Tue Aug 20 20:34:39.628315 2024] [php:warn] [pid 22] [client 172.18.0.1:58420] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2, referer: http://localhost:8080/pl/
2024-08-20 22:34:39 [Tue Aug 20 20:34:39.634230 2024] [php:warn] [pid 19] [client 172.18.0.1:58460] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2, referer: http://localhost:8080/pl/
2024-08-20 22:34:41 [Tue Aug 20 20:34:41.947763 2024] [php:warn] [pid 19] [client 172.18.0.1:58460] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2, referer: http://localhost:8080/pl/
2024-08-20 22:34:43 [Tue Aug 20 20:34:43.333291 2024] [php:warn] [pid 19] [client 172.18.0.1:58460] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2, referer: http://localhost:8080/pl/moje-konto
2024-08-20 22:34:55 [Tue Aug 20 20:34:55.280650 2024] [php:warn] [pid 28] [client 172.18.0.1:44394] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2, referer: http://localhost:8080/pl/moje-konto
2024-08-20 22:34:56 [Tue Aug 20 20:34:56.622194 2024] [php:warn] [pid 28] [client 172.18.0.1:44394] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2, referer: http://localhost:8080/pl/dane-osobiste
2024-08-20 22:35:00 [Tue Aug 20 20:35:00.060001 2024] [php:warn] [pid 28] [client 172.18.0.1:44394] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2, referer: http://localhost:8080/pl/dane-osobiste
2024-08-20 22:35:00 [Tue Aug 20 20:35:00.769979 2024] [php:warn] [pid 28] [client 172.18.0.1:44394] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2, referer: http://localhost:8080/pl/moje-konto
2024-08-20 22:35:02 [Tue Aug 20 20:35:02.922552 2024] [php:warn] [pid 28] [client 172.18.0.1:44394] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2, referer: http://localhost:8080/pl/moje-konto
2024-08-20 22:35:03 [Tue Aug 20 20:35:03.251881 2024] [php:warn] [pid 28] [client 172.18.0.1:44394] PHP Warning:  include_once(/var/www/html/modules/testmodule/controllers/front/test.php): Failed to open stream: No such file or directory in /var/www/html/classes/Dispatcher.php on line 389, referer: http://localhost:8080/pl/moje-konto
2024-08-20 22:35:03 [Tue Aug 20 20:35:03.252027 2024] [php:warn] [pid 28] [client 172.18.0.1:44394] PHP Warning:  include_once(): Failed opening '/var/www/html/modules/testmodule/controllers/front/test.php' for inclusion (include_path='/var/www/html/vendor/pear/pear_exception:/var/www/html/vendor/pear/console_getopt:/var/www/html/vendor/pear/pear-core-minimal/src:/var/www/html/vendor/pear/archive_tar:.:/usr/local/lib/php') in /var/www/html/classes/Dispatcher.php on line 389, referer: http://localhost:8080/pl/moje-konto
2024-08-20 22:35:03 [Tue Aug 20 20:35:03.252454 2024] [php:error] [pid 28] [client 172.18.0.1:44394] PHP Fatal error:  Uncaught Error: Class "testmoduletestModuleFrontController" not found in /var/www/html/classes/controller/Controller.php:246\nStack trace:\n#0 /var/www/html/classes/Dispatcher.php(504): ControllerCore::getController('testmoduletestM...')\n#1 /var/www/html/index.php(28): DispatcherCore->dispatch()\n#2 {main}\n  thrown in /var/www/html/classes/controller/Controller.php on line 246, referer: http://localhost:8080/pl/moje-konto

The directions looked like this:
start container -> enter localhost:8080 -> my account -> my information (random entry) -> my account -> link test what is there now

Link to comment
Share on other sites

Hello,

That fatal error in there is most likely causing the 500.

That's my bad, I did not see you are not using the module name in the class name as well. As mentioned here, the structure for the class name needs to be the following: <ModuleClassName><FileName>ModuleFrontController, so it needs to be something like TestModuleTestModuleFrontController

Link to comment
Share on other sites

Docker Presta container:

2024-08-22 21:27:49 [Thu Aug 22 19:27:49.088739 2024] [php:warn] [pid 25] [client 172.18.0.1:60270] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2, referer: http://localhost:8080/pl/module/testmodule/TestTestControllerFrontController
2024-08-22 21:27:49 172.18.0.1 - - [22/Aug/2024:19:27:49 +0000] "GET /pl/moje-konto HTTP/1.1" 200 11877 "http://localhost:8080/pl/module/testmodule/TestTestControllerFrontController" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-22 21:27:49 [Thu Aug 22 19:27:49.831848 2024] [php:warn] [pid 25] [client 172.18.0.1:60270] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2, referer: http://localhost:8080/pl/moje-konto
2024-08-22 21:27:49 172.18.0.1 - - [22/Aug/2024:19:27:49 +0000] "GET /pl/module/blockwishlist/action?action=getAllWishlist HTTP/1.1" 200 509 "http://localhost:8080/pl/moje-konto" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-22 21:27:52 [Thu Aug 22 19:27:52.171186 2024] [php:warn] [pid 25] [client 172.18.0.1:60270] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2, referer: http://localhost:8080/pl/moje-konto
2024-08-22 21:27:52 172.18.0.1 - - [22/Aug/2024:19:27:52 +0000] "GET /pl/module/testmodule/TestTestControllerFrontController HTTP/1.1" 404 51670 "http://localhost:8080/pl/moje-konto" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-22 21:27:52 [Thu Aug 22 19:27:52.808770 2024] [php:warn] [pid 25] [client 172.18.0.1:60270] PHP Warning:  Constant _PS_MODE_DEV_ already defined in /var/www/html/modules/testmodule/testmodule.php on line 2, referer: http://localhost:8080/pl/module/testmodule/TestTestControllerFrontController
2024-08-22 21:27:52 172.18.0.1 - - [22/Aug/2024:19:27:52 +0000] "GET /pl/module/blockwishlist/action?action=getAllWishlist HTTP/1.1" 200 509 "http://localhost:8080/pl/module/testmodule/TestTestControllerFrontController" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-22 21:29:04 172.18.0.1 - - [22/Aug/2024:19:29:04 +0000] "POST /admin4577/index.php/common/notifications?_token=XuyJm1VTeWfzcj_292PPOpl3meKu_-xLocA8FF79oBg&rand=1724354944304 HTTP/1.1" 200 421 "http://localhost:8080/admin4577/index.php/configure/shop/seo-urls/49/edit?_token=XuyJm1VTeWfzcj_292PPOpl3meKu_-xLocA8FF79oBg" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
2024-08-22 21:29:04 172.18.0.1 - - [22/Aug/2024:19:29:04 +0000] "POST /admin4577/index.php/common/notifications?_token=XuyJm1VTeWfzcj_292PPOpl3meKu_-xLocA8FF79oBg HTTP/1.1" 200 421 "http://localhost:8080/admin4577/index.php/configure/shop/seo-urls/49/edit?_token=XuyJm1VTeWfzcj_292PPOpl3meKu_-xLocA8FF79oBg" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"

modules/testmodule/controllers/admin/AdminTestController.php

<?php

class AdminTestController extends ModuleAdminController
{
    public function __construct()
    {
        parent::__construct();
        $this->bootstrap = true;
    }

    public function initContent()
    {
        parent::initContent();
        
        $this->context->smarty->assign([
            'content' => 'TEST'
        ]);

        $this->setTemplate('test.tpl');
    }
}

modules/testmodule/controllers/front/TestController.php

<?php

class TestTestControllerFrontController extends ModuleFrontController
{

    public function __construct() {
        parent::__construct();
    }

    public function init()
    {
        $this->page_name = 'myfunction';
        $this->display_column_left = false;
        $this->display_column_right = false;
        parent::init();
    }

    public function initContent()
    {
        parent::initContent();

        $this->context->smarty->assign([
            'message' => 'TEST'
        ]);

        $this->setTemplate('module:testmodule/views/templates/front/test.tpl');
    }
}

modules/testmodule/testmodule.php

<?php
define('_PS_MODE_DEV_', true);
if (!defined('_PS_VERSION_')) {
    exit;
}

class TestModule extends Module
{
    public function __construct()
    {
        $this->name = 'testmodule';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'Your Name';
        $this->need_instance = 0;
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Test Module');
        $this->description = $this->l('A simple module to display TEST in admin and user account.');

        $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
    }

    public function install()
    {
        return parent::install() &&
            $this->installTab() &&
            $this->registerHook('displayCustomerAccount') &&
            $this->registerHook('moduleRoutes');
    }

    public function uninstall()
    {
        return parent::uninstall() &&
            $this->uninstallTab();
    }

    protected function installTab()
    {
        $tab = new Tab();
        $tab->active = 1;
        $tab->class_name = 'AdminTest';
        $tab->name = [];
        foreach (Language::getLanguages(true) as $lang) {
            $tab->name[$lang['id_lang']] = 'Test';
        }
        $tab->id_parent = (int)Tab::getIdFromClassName('AdminParentOrders');
        $tab->module = $this->name;
        $tab->icon = 'settings';
        return $tab->add();
    }

    protected function uninstallTab()
    {
        $id_tab = (int)Tab::getIdFromClassName('AdminTest');
        if ($id_tab) {
            $tab = new Tab($id_tab);
            return $tab->delete();
        }
        return true;
    }

    public function hookDisplayCustomerAccount()
    {
        $this->context->smarty->assign([
            'testPageUrl' => $this->context->link->getModuleLink('testmodule', 'TestTestControllerFrontController')
        ]);
        return $this->display(__FILE__, 'views/templates/front/account-tab.tpl');
    }

    public function hookModuleRoutes($params)
    {
        return [
            'module-testmodule-test' => [
                'controller' => 'test',
                'rule' => 'test',
                'keywords' => [],
                'params' => [
                    'fc' => 'module',
                    'module' => 'testmodule',
                ]
            ]
        ];
    }
}

 

Templates:
modules/testmodule/views/templates/admin/test.tpl

<div class="panel">
    <h1>{$content} X</h1>
</div>

 

modules/testmodule/views/templates/front/account-tab.tpl

<li class="item">
    <a href="{$testPageUrl}" title="{l s='Test' mod='testmodule'}">
        {l s='Test Page' mod='testmodule'} {$testPageUrl}
    </a>
</li>

 

modules/testmodule/views/templates/front/test.tpl

<h1>TEST</h1>

 

Not much has changed 😕
Maybe some of you have a simple module like this that you can follow? Because this problem, which seems simple, is tiring :/.
I still add docker-compose, maybe something is wrong here:

version: '3'
services:
#  phpmyadmin:
#    container_name: phpmyadmin
#    links:
#      - some-mysql
#    image: phpmyadmin/phpmyadmin:latest
#    restart: always
#    environment:
#      PMA_HOST: some-mysql
#      PMA_PORT: 3306
#      PMA_ARBITRARY: 1
#      restart: unless-stopped
#    ports:
#      - 8081:80
#  nodejs:
#    build:
#      context: .
#      dockerfile: Dockerfile
#    container_name: presta-dev
##    volumes:
##      - ./presta-dev:/var/www/html/themes/combatTheme/_dev
##      - ./presta-dev:/_dev:delegated
#    depends_on:
#      - prestashop
#    command: docker-sync-stack start && npm run watch
#    volumes:
#      - dev-sync:/_dev
#    networks:
#      - prestashop_network
#    tty: true
  mysql:
    container_name: some-mysql
    image: mysql:5.7
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: admin
      MYSQL_DATABASE: prestashop
    networks:
      - prestashop_network
    volumes:
      - dbdata:/var/lib/mysql
  prestashop:
    container_name: combatShootingShop
    image: prestashop/prestashop:8.1.1
#    build: .
    restart: unless-stopped
    depends_on:
      - mysql
    ports:
      - 8080:80
    environment:
      DB_SERVER: some-mysql
      DB_NAME: prestashop
      DB_USER: root
      DB_PASSWD: admin
      PS_FOLDER_ADMIN: admin4577
      PS_FOLDER_INSTALL: install4577
      PS_INSTALL_AUTO: 1
      PS_LANGUAGE: pl
      PS_COUNTRY: PL
      PS_DOMAIN: localhost:8080
      ADMIN_MAIL: [email protected]
      ADMIN_PASSWD: prestashop_demo
      PS_MODE_DEV: true
    networks:
      - prestashop_network
    volumes:
      - type: bind
        source: ./modules # local path to the module
        target: /var/www/html/modules # path to be mounted in the container
      - type: bind
        source: ./themes/classic  # local path to the theme
        target: /var/www/html/themes/classic # path to be mounted in the container
#      - type: bind
#        source: ./config  # local path to the theme
#        target: /var/www/html/config # path to be mounted in the container
      - psdata:/var/www/html
networks:
  prestashop_network:
volumes:
  psdata:
  dbdata:


 

structure.png

404.png

Link to comment
Share on other sites

On 8/21/2024 at 12:50 PM, Nickz said:

Is your server set up for a fully qualified domain name?
Do you use a shell to go into the server?

type hostname
or hostnamectl

for most frequently used distros.

What is the result on the hostname inquiry?

Link to comment
Share on other sites

Hello,

As mentioned previously, the class name of the front controller must be prefixed with the module class name. It must be TestModuleTestModuleFrontController - the module class name (TestModule), the controller name (Test) and the 'ModuleFrontController' suffix.

The filename also needs to be test.php - you can also look into other default modules to see how they are implementing front controllers.

Last, but not least, the second parameter for the getModuleLink method needs to be the controller name, which is 'test' -> $this->context->link->getModuleLink('testmodule', 'test')

If the above still do not work for you, just let me know, and I'll create a quick module that will showcase how you can create a front controller and link it to the customer account page.

  • Thanks 1
Link to comment
Share on other sites

@Andrei H

OK!.thumb.png.18d35d67281cc9d171f2292c037c1ab3.png

It worked!
I just don't know why my controller file couldn't be named

TestController.php

 

But ok. I extended the .tpl file with page.tpl to give this page some appearance

{extends file='customer/page.tpl'}

{block name='page_title'}
    {l s='TEST_PAGE'}
{/block}

{block name='page_content'}
    <h1>Test XXXX</h1>
{/block}

 

Link to comment
Share on other sites

Hello,

The front controllers are still working with the old logic. The file name must be the name of the controller - usually, you will see it being written in camelCase.

You should be able to name it 'testController.php' as well, but at that point the the class name has to be TestModuleTestControllerModuleFrontController - and you should be able to get the url for it with $this->context->link->getModuleLink('testmodule', 'testController')

  • Like 1
Link to comment
Share on other sites

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...