Currently Online

We have 22 guests online
None

Login

             | 

Search

Like it? Share it!

A Modular Directory Structure Quickstart: Configure The Application

webdev - Zend Framework Tutorials

User Rating: / 9
PoorBest 

Thanks for joining me in the third installment of this tutorial series on Zend_Application.  In this tutorial we are going to create your .ini file and configure it to function correctly with your application.

Zend_Application comes with a lot of ready to use Resource plugins.  The objective here is to use the resource plugins and keep your bootstrap relatively clean.  It is actually advisable to create your own resource plugins customized for your application, but for now we'll use the defualt ones shipped with Zend_Application.

The relevant documentation I'd really like you to read:

create and/or edit application/config/config.ini

First we are going to simply create our relevant sections in the config file.

[production]
[development : testing]
[development : production]

Zend_Application provides the base functionality of the component, and the entry point to your Zend Framework application. It's purpose is two-fold: to setup the PHP environment (including autoloading), and to execute your application bootstrap.  Typically, you will pass all configuration to the Zend_Application constructor, but you can also configure the object entirely using its own methods.

Next we are going to set some php settings and defaults. Please modify these settings to suit your needs. APPLICATION_PATH, MODULE_PATH, LIB_PATH were some constants we defined earlier in public/index.php in the tutorial series, Part 1.  These can be used here.  As you can see I explicity set Zend and ZendX namespaces even though they are already registered by the bootstrap.  You need only specify any additional libraries you wish to autoload.  I did this just to show you an example.

As you can see, we have our basic configuration in place.  The Application is still not fully functional. We have a few more things to take care of.

[production]
;error reporting
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
;set timezone
phpsettings.date.timezone = "America/Chicago"
;include path
includePaths.library = APPLICATION_PATH "/../library"
;bootstrap
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
;autoloadernamespaces
autoloadernamespaces.0 = "App_"
autoloadernamespaces.1 = "Zend_"
autoloadernamespaces.2 = "ZendX_"


[development : testing]
;error reporting
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1


[development : production]
;error reporting
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

Now we need to tell our configuration file about our front controllers, and for it to throw exceptions. Lets edit the above file to look like the following: (the difference is empasized via bold.)

[production]
;error reporting
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
;set timezone
phpsettings.date.timezone = "America/Chicago"
;include path
includePaths.library = APPLICATION_PATH "/../library"
;bootstrap
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
;autoloadernamespaces
autoloadernamespaces.0 = "App_"
autoloadernamespaces.1 = "Zend_"
autoloadernamespaces.2 = "ZendX_"
;resources.frontController
resources.frontController.env = APPLICATION_ENV
resources.frontController.throwerrors = false



[development : testing]
;error reporting
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
;resources.frontController
resources.frontController.throwerrors = true



[development : production]
;error reporting
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
;resources.frontController
resources.frontController.throwerrors = true


Alright, for now, we're done with the testing and the development sections.  Basically what we did was tell our frontcontroller resource (which is already registered via Zend_Applicatin's Bootstrap) to throw exceptions only if the APPLICATION_ENV is set to testing or development, which you should set in your .htaccess file.

What we want to do now, is to let our front controller resource know that we want to use modules.  If you recall from the pre 1.8 days, we had to use the following methods to accomplish this.  This hasn't changed, you can specify these settings via a php array(), or with the ini file.  Let's use the ini file.

public static function setupFrontController()
    {
        self::$frontController->setControllerDirectory(
            array(
                'default' => self::$root . '/application/controllers',
                'admin' => self::$root . '/application/admin/controllers'
            )
        );
    }

Which is specified like this, but this isn't really neccessery as the setModuleDirectory() method should be used instead.

resources.frontController.controllerDirectory.default = MODULE_PATH "/default/controllers"
resources.frontController.controllerDirectory.admin = MODULE_PATH "/admin/controllers"


public static function setupFrontController()
    {
        self::$frontController->setModuleDirectory('application/modules/');
    }

Which is then specified like this.

resources.frontController.moduleDirectory = MODULE_PATH

Lets go ahead and seup our entire Front Controller using the .ini file.

[production]
;resources.frontController
resources.frontController.moduleDirectory = MODULE_PATH
resources.frontController.moduleControllerDirectoryName = "controllers"

resources.frontController.plugins.moduleswitcher = "App_Controller_Plugin_ModuleSwitcher"
resources.frontController.env = APPLICATION_ENV
resources.frontController.throwerrors = false

resources.frontController.defaultModule = "default"
resources.frontController.defaultAction = "index"
resources.frontController.defaultControllerName = "index

The bits in BOLD are very important.  Now we must register our front controller plugin.  Following PEAR paths, you can load any number of plugins this way.

Now we can setup our Database Resource.

;resources.database
resources.db.params.adapter = PDO_MYSQL
resources.db.isdefaulttableadapter = true
resources.db.params.host =
resources.db.params.username =
resources.db.params.password =
resources.db.params.dbname =

And you can even have module specific resources, for example:

;resources.database.default
default.resources.db.params.adapter = PDO_MYSQL
default.resources.db.isdefaulttableadapter = true
default.resources.db.params.host =
default.resources.db.params.username =
default.resources.db.params.password =
default.resources.db.params.dbname =

;resources.database.admin
admin.resources.db.params.adapter = PDO_MYSQL
admin.resources.db.isdefaulttableadapter = true
admin.resources.db.params.host =
admin.resources.db.params.username =
admin.resources.db.params.password =
admin.resources.db.params.dbname =

Lets go ahead and setup our Locale.

;resources.locale
resources.locale.default = en_US

Now your application should be running.  Our final config should look like the following, given you didn't customize it too heavily.

[production]
;error reporting
        phpSettings.display_startup_errors = 0
        phpSettings.display_errors = 0

;set timezone
        phpsettings.date.timezone = "America/Chicago"

;include path
        includePaths.library = APPLICATION_PATH "/../library"

;bootstrap
        bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
        bootstrap.class = "Bootstrap"
;autoloader
        autoloadernamespaces.0 = "App_"
        autoloadernamespaces.1 = "Zend_"
        autoloadernamespaces.2 = "ZendX_"

;resources.frontController
        resources.frontController.moduleDirectory = MODULE_PATH
        resources.frontController.moduleControllerDirectoryName = "controllers"

        resources.frontController.plugins.moduleswitcher = "App_Controller_Plugin_ModuleSwitcher"
        resources.frontController.env = APPLICATION_ENV
        resources.frontController.throwerrors = false

        resources.frontController.defaultModule = "default"
        resources.frontController.defaultAction = "index"
        resources.frontController.defaultControllerName = "index"

;resources misc
        resources.locale.default = en_US

;resources.database
        resources.db.params.adapter = PDO_MYSQL
        resources.db.isdefaulttableadapter = true
        resources.db.params.host =
        resources.db.params.username =
        resources.db.params.password =
        resources.db.params.dbname =

[testing : production]
;error reporting
        phpSettings.display_startup_errors = 1
        phpSettings.display_errors = 1


;resources.frontController
        resources.frontcontroller.throwerrors = true

[development : production]
;error reporting
        phpSettings.display_startup_errors = 1
        phpSettings.display_errors = 1


;resources.frontController
        resources.frontcontroller.throwerrors = true

Now our application should be 100% functional. Go ahead, load it up in the browser. If you have configured everything correctly you should be able to see http://localhost/sample/public/index.php/default/index (to be precise) and in general http://localhost/sample/public/ would call the default module, as thats the one we setup as our default.  Go ahead and now go to http://localhost/sample/public/index.php/admin/index and you will see the admin layout and index controller identify themselves to you.

I am feeling really tired right now. I have typed up enough for now. Try to get your heads around this. If you have any questions, you can contact me here via comments or on irc.freenode.net #zftalk

My Twitter

atirjavid's avatar
Atir Javid atirjavid
  • bio:
    C.E.O Frosted Web, Social Media Marketing & Development | C.T.O. Sky Social Media Inc, former Debian contributor, maintainer and developer. Currently w/FreeBSD.
  • web:
  • location:
    Baton Rouge, LA. USA
  • updates:
    389
  • followers:
    136
  • following:
    15
Last 4 following:
runev's avatar emiltofan's avatar CalEvans's avatar fabpot's avatar
Loading...

Last 0 tweets in past 30 days from atirjavid:

People talking about '@Linux':

Quick Links

CopyLeft Notice

Distribution of the Original Works

You’re free to copy and redistribute the copylefted work — either by itself, or as part of a larger collection of works. All you have to do is (1) be honest - by clearly crediting me as the author, and only redistributing accurate copies of the original (But you don't REALLY have to :) hehe )- and (2) keep it free - by distributing the copy under the same copyleft license, and including either a copy of the license, or a reference to its URL (http://creativecommons.org/licenses/by-sa/1.0/legalcode).

You don’t have to copyleft your whole newsletter to reprint a single article of mine in it (although copylefting your whole newsletter is certainly worthwhile on its own merits). You only have to distribute the copy of my work under the copyleft terms. You’re also free to distribute abridged, translated, or otherwise edited copies of my works — but these count as derivative works, not copies of the original.

Derivative Works

You’re also free to make and distribute derivative works from the copylefted work (such as abridged or edited copies of original works, stories about characters introduced in the original works, etc.). All you have to do is (1) be honest — by clearly identifying your work as a derivative work, and clearly crediting me as the author of the original work on which it was based (But you don't REALLY have to do that either :) Hehehe) — and (2) keep it free — by distributing your derivative work as free content under the terms of the same copyleft license under which the original work was distributed. Please read the (The GNU General Public License ). Thank you very much.

Credit me or don't. Just please keep the information free and flowing.