Part 2 structure:
- Composer & namespaces brief theory
- Installing Composer
- Creating module directory with composer.json file
- Explaining composer.json file structure
- (For Windows users) Installing Cmder
- Composer autoload
Composer & namespaces brief theory
If you have ever heard of the Composer and worked with PrestaShop 1.6 or older, you have certainly noticed that it is used very rarely. Of course, you could use it in your module, but it took a lot of effort and workaround to make it work.
Fortunately it’s more common now to use this great tool in PS 1.7 – because even some official PrestaShop modules are using Composer. Furthermore, 1.7 has defined namespaces, so you can refer to them in your module files.
For those who do not know what a composer is, I hurry with an explanation. Its official website says it’s:
a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.https://getcomposer.org/doc/00-intro.md
So you can include 3rd party packages (libraries) in your Composer configuration file (called composer.json), but is it necessary? Not always. In our case, “helpdesk tutorial” module will not use any external dependency, because there is no need. However, the main reason for having Composer in your module is you can create your namespace definitions easily – using that composer.json file.
If you are not familiar with namespaces, I recommend reading this article
I will explain it also later, but this link above may help you with better understanding.
If you didn’t install Composer, grab it here
https://getcomposer.org/download/ and install on your local PC.
Creating module directory with composer.json file
Next, go to your PrestaShop 1.7.6 modules directory and create a folder named
helpdesktutorial. Then, create empty
composer.json file inside this directory. Edit this file and paste following text:
"description": "Help developers to understand how to create module using PrestaShop 1.7 (up to 1.7.6) and it's new features",
Explaining composer.json file structure
I think it looks clear:
- name is usually
author/modulename(like in GitHub)
- description should be short and can be the same as module description in PrestaShop
- autoload is where our “namespace magic” happens; you can name it as you want – but the best would be
Author\\ModuleName\\(PascalCase notation) pointing to
- type – should be
(For Windows users) Installing Cmder
Cmder is wonderful console emulator for Windows. It’s looks better than standard built-in console command line interface and offers many additional features.
You can download it here: https://cmder.net/.
Install and open Cmder. Go to your module directory using
ls -land press enter. If you want to go 1 level higher, type
If your console points to your “helpdesk tutorial” module directory, try using following command:
composer dump-autoload -o
If everything works, you will see a message like this:
Generated optimized autoload files containing 0 classes
For now it’s useless, but you should use this command often to recreate list of autoload classes used in “helpdesk tutorial” module project.
Click to see file state you should have from this part
That’s enough from this part!