Jump to content

Creating A Module Example...


Recommended Posts

So I recently downloaded the latest version of PrestaShop because I heard good things about its template system and easy integration of modules. I have been following the tutorial for creating a module located at http://doc.prestasho...estaShop+module. I'm not very experienced at development, having mostly done design with some basic PHP functions, so this might sound really dumb, but I've run into a few issues.

 

1. When I tried to install the module I kept getting an error saying that my version of PrestaShop was not compatible with my module. I removed this line from the constructor method and it installed just fine:

$this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.5'); 

 

2. When I tried to add the display.php page for the link it kept generating a 404 error. The path is being generated by this line of code in the hookDisplayLeftColumn function:

'my_module_link' => $this->context->link->getModuleLink('mymodule', 'display')

The path generated was: module/mymodule/display

Since the modules are actually kept in the 'modules' (with the 's') directory, the path needed to be: modules/mymodule/display

The getModuleLink function uses a variable in the Dispatcher.php file called $default_routes. Inside this array is a key called 'module'. The code is on lines 110-120:

'module' => array(
		'controller' =>	null,
		'rule' =>		'module/{module}{/:controller}',
		'keywords' => array(
			'module' =>			array('regexp' => '[_a-zA-Z0-9_-]+', 'param' => 'module'),
			'controller' =>		array('regexp' => '[_a-zA-Z0-9_-]+', 'param' => 'controller'),
		),
		'params' => array(
			'fc' => 'module',
		),
	),

The only way I can get the link in the example to work is if I change this line in the core file to add the 's':

'module' => array(
		'controller' =>	null,
		'rule' =>		'module[color=#ff0000]s[/color]/{module}{/:controller}',
		'keywords' => array(
			'module' =>			array('regexp' => '[_a-zA-Z0-9_-]+', 'param' => 'module'),
			'controller' =>		array('regexp' => '[_a-zA-Z0-9_-]+', 'param' => 'controller'),
		),
		'params' => array(
			'fc' => 'module[color=#ff0000]s[/color]',
		),
	),

This allowed the page to load as long as I had clean URLs on. With no clean URLs I still got the 404 error.

 

3. The tutorial creates the link with no .php extension. This also generated a 404 error. Everything I read said that PrestaShop 1.5 does not need the extensions. I don't know how this is being handled since I couldn't find any re-writes in the .htaccess file that took care of this issue. The only way I could get it to work was to create a directory level .htaccess in the mymodule folder (adding the re-write to the main .htaccess caused other problems):

#Set RewriteEngine
RewriteEngine On
RewriteBase /modules/mymodule/

#Rewrite files to remove extentions
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !.*\.php
RewriteRule ^(.*)$ $1.php [L]

 

4. The fixes on 2 and 3 above allowed me to display the generic 'Welcome to this page!' page. However, they did not allow me to continue the tutorial. When I tried to implement the template and change the display.php file to extend the ModuleFrontController class I get a 500 error. Do the changes above remove the display.php from the PrestaShop framework when clean URLs are on?

 

I thought everything was going along fine and then I ran into these issues and I have no idea how to resolve them. Any help would be greatly appreciated since I am trying to get a handle on modules but can't even make it through the tutorial without issues. Maybe I don't even need to do a custom module...what I am trying to do is put a banner under the header which displays a random image from a folder every time the page loads. On my old site, without ecommerce, I had a PHP function to read the directory and randomly choose an image to display. However, with the template system using Smarty, I can't find a way to include this function without creating a module.

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

×
×
  • Create New...