The Heart of Your Product

Documentation

Download in PDF

Download local copy of SCore docs in one file

ZF Documentation

Read It

Explore ZF manuals for native features

Table of Contents

Create Application

This chapter describes application and module directory structure. Also you can learn how to create your own module step by step.

Directory structure

Application structure

Directory/file

Description

configs

Application configuration directory.

The default configuration is placed in application/configs/application.ini, and contains some basic directives for setting your PHP environment (for instance, turning error reporting on and off), indicating the path to your bootstrap class (as well as its class name), and the path to your action controllers.

layouts

Contains common layouts like error.phtml for error handler.

localization

For storage modules language translations.

Example:

|_ en
  |_ contacts.php – translation for module "contacts"
  |_ news.php      – translation for module "news"
  |_ system.php   – translation for module "system"
|_ de
|_ es
|_ fr

modules

Modules directory.

temp

Temporary files directory.

Directory

Description

cache

Cache directory. Used for storing locally cached files.

logs

Contains txt log files.

sessions

Session save path directory.

templates

Contains common templates.

Bootstrap.php

Application bootstrap.

Module structure

Module structure

Each module should observe the convention of directories. Next informational table describes it.

Directory

Description

components

Components directory contains module components that can be used directly in other modules. Usually needed for display cross-module information blocks, widgets, text information, forms (such as login form) and so on. You can run components from any place in your application: controllers, models, views, bootstraps, libraries if it needed.

While creating component simply extend your class with Singular_Component_Abstract. To execute component use following code example: Singular_Component::run('CompName', 'module', $compOptions);

controllers

Your application's action controllers contain your application workflow, and do the work of mapping your requests to the appropriate models and views.

Directory

Description

Abstract

"Abstract" folder needed for storing abstract controller classes with basic actions functional and serving for extending main module controllers.

Example:

Abstract controller module/controllers/Abstract/Index.php:

abstract class Test_Controller_Abstract_Index extends Singular_Controller_Action
{
    public function indexAction ()
    {
        // page title
        $this->title('News adding');

        // initialize form
        $this->view->form = new Test_Form_News();

        $this->template('news');
    }
}

Main controller module/controllers/IndexController.php:

class Test_IndexController extends Test_Controller_Abstract_Index
{
}

If programmer needs to modify or redefine default abstract controllers functionality the following actions is a good practice:

class Test_IndexController extends Test_Controller_Abstract_Index
{
    public function indexAction ()
    {
        // page title
        $this->title('Hot news management');

        // initialize form
        $this->view->form = new Test_Form_News();

        $this->template('newsAdd');
    }
}

Then if we update module to higher version all of custom modifications will stay untouched.

Never modify abstract controllers if you are not developer of this module. This may cause code collisions and impossibility to make correct module update. Redefine normal controller actions instead.

forms

Form classes that will be used to render html forms including various filters and validators in templates.

handlers

Directory for storing event handlers. Classes must be an instance of Singular_Event_Listener_Abstract. See Events & Handlers chapter for information how to create and use event handlers.

helpers

Directory for storing view helpers that will be used by current module.

models

Store your models and mappers in the root of "models" folder.

Directory

Description

DbTable

Store your database table models here. Each model class must be an instance of Singular_Db_Table_Abstract.

templates

View scripts directory. The filename convention for auto-rendering is following:

moduleName_controllerName_actionName.phtml.

You can also use arbitrary filename for your module templates, for example:
dashboard.phtml. For manual template rendering use $this->template('dashboard') in controller. If you have specified template file extension $this->template('dashboard.phtml') this is also correct way.

lang.php

Language keys file in php array format.

Module.php

Module information file.

routes.ini

Route definitions in ini format.

Example:

portfolio_category.type = Zend_Controller_Router_Route_Regex
portfolio_category.route = "category(\d+)/portfolio(\d{0,})\.html"
portfolio_category.defaults.module = portfolio
portfolio_category.defaults.controller = frontend
portfolio_category.defaults.action = index
portfolio_category.map.1 = category_id
portfolio_category.map.2 = page
portfolio_category.reverse = "category%s/portfolio%s.html"

Module name

Module name can consist with letters, digits, - and _ symbols, but not only with digits and -, _ symbols.

Correct module names:

  • search
  • listings
  • look4ward
  • support24
  • 4x4wheels
  • hot-tours

Bad module names:

  • jack_daniels
  • bob&joe store
  • ruby on rails
  • 100%respect
  • sport#major
  • club$metro
  • 44400222
  • 1+3=4

For example we take "test" (without quotes) name for our module.

Add an empty directory with name "test" as your module name into application/modules directory.

Adding module controllers

Add "controllers" directory to the module as seen on "Image 2".

Image 2. Module controller directory

Let's create Index Controller as your main module controller that should handle some actions. See "Image 3".

Image 3. Creating Index controller

Firstly you need to create abstract controller application/modules/test/controllers/Abstract/Index.php as follows:

abstract class Test_Controller_Abstract_Index extends Singular_Controller_Action
{
/**
  * Index action
  *
  * @return void
 */
 public function indexAction()
 {
  // action code body will be here
 }
} 

Then create simple controller application/modules/test/controllers/IndexController.php and extend it from abstract controller Test_Controller_Abstract_Index. This controller can't be modified except this module authors. Use simple controller instead.

URLs:

/**
* Index controller for test module
*
* @see Test_Controller_Abstract_Index
*/
class Test_IndexController extends Test_Controller_Abstract_Index
{
}

Prefix "Test_" in simple controller class name specifies in an accessory to the module "test". This will be an empty class that only inherits actions from Test_Controller_Abstract_Index. Developers can use this controller to modify default actions from abstract controller.

SingularCORE provides to developers two controller types such as "frontend" and "admin". Example controller above has public "frontend" controller type. Lets see how to create "admin" controller type.

To create admin controller you only need to specify "Admin" prefix in your controller name.

Create abstract admin controller application/modules/test/controllers/Abstract/Admin.php:

abstract class Test_Controller_Abstract_Admin extends Singular_Controller_Action
{
    /**
    * Index action
    *
    * @return void
    */
    public function indexAction()
    {
    // action code body will be here
    }
}

After create simple controller application/modules/test/controllers/AdminController.php:

URLs:

/**
* Index controller for test module
*
* @see Test_Controller_Abstract_Admin
*/
class Test_AdminController extends Test_Controller_Abstract_Admin
{
}

Thats all! Now you have public "frontend" controller and "admin" controller.

If you need additional admin controller simply add "Admin" prefix to controller name application/modules/test/controllers/Abstract/AdminCategories.php.

abstract class Test_Controller_Abstract_AdminCategories extends Singular_Controller_Action
{
    /**
    * List action
    *
    * @return void
    */
    public function listAction()
    {
    // action code body will be here
    }
}

And finally simple controller application/modules/test/controllers/AdminCategoriesController.php:

URLs:

/**
* AdminCategories controller for test module
*
* @see Test_Controller_Abstract_AdminCategories
*/
class Test_AdminCategoriesController extends Test_Controller_Abstract_AdminCategories
{
}