Question: What are the basic steps for create new module?
Suppose you are creating module with name of Album.
- Attach module with application.
Open file i.e /config/application.config.php, Add Blog in array like below:return array( 'modules' => array( 'Application', 'Blog' ), );
- create Album folder inside the module folder.
- After creating the Album folder inside modules, Create below folder structure.
Album/ Module.php autoload_classmap.php autoload_function.php autoload_register.php config/ module.config.php public/ images/ css/ js/ src/ Album/ Controller/ AlbumController.php Form/ AlbumForm.php Model/ Album.php view/ album/ album/ index.phtml layout/ layout.phtml error index.phtml
Question: How to attach a module in main application?
Go to File config/application.config.php, and add your module name in modules array.
See example below (We have add a module Album).
return array( 'modules' => array( 'Application', 'Album' ), );
Question: What is Service?
A Service is an object in the module that executes complex logic of the application.
Here we do all difficult logic together and gives you easy to understand results.
Question: What are the basic steps for creating the Service?
-
Create a folder service in-side the module and path will be similar to below:
/module/{moduleName}/src/{moduleName}/Service/{Album}Service.php
-
Create a model in-side the module and path will be similar to below:
/module/{moduleName}/src/{moduleName}/Model/Post.php
-
Add Following code in controller where you want to use.
use {moduleName}\Service\PostServiceInterface;
Add following in controller (you can use another variable)protected $postService; public function __construct(PostServiceInterface $postService) { $this->postService = $postService; }
- Create the Factory class (path: /module/{moduleName}/src/{moduleName}/Factory/{factoryClass}.php)
-
Register your service in module.config.php. (Add following code)
'service_manager' => array( 'invokables' => array( '{moduleName}\Service\{Service}' => '{moduleName}\Service\{Service}' )
- Register your service from module.config
(Path: /module/Blog/config/module.config.php)'controllers' => array( 'factories' => array( '{moduleName}\Controller\List' => '{moduleName}\Factory\{factoryClass}' )
There are different module.config.php file inside each module (module/{moduleName/config/module.config.php}.
In this you you can set the database credentials.
'db' => array( 'driver' => 'Pdo', 'username' => 'SECRET_USERNAME', //edit this 'password' => 'SECRET_PASSWORD', //edit this 'dsn' => 'mysql:dbname=blog;host=localhost', 'driver_options' => array( \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' ) ),