开发者

Where's the best place to store application specific non-model classes in a Zend Framework app?

Given that the model is a "domain-specific representation of the data upon which the application operates." [Wikipedia: MVC], service, form, plugin classes, etc. aren't considered part of the model, so they go in their own directories under /application. The default resource auto-loader sets this up for us, so MyApp_Form_Login is automatically found in /application/form/Login.php.

For my application I need to write a custom authentication adapter. The logic in it will be application-specific, so it's not reusable library code, therefore it doesn开发者_StackOverflow社区't belong in /library/MyApp. It's not a service class, so it doesn't belong in /application/service, nor a form, etc. So, idiomatically, where should this class be stored?


You can create additional folders in your application folder and add a resource path in your Bootstrap class.

For example, assuming that you use a namespace for all your application-specific classes (models, forms, plugins, etc) of 'Application', you could use the following:

protected function _initAutoloader()
{
    $autoloader = new Zend_Application_Module_Autoloader(array(
            'basePath' => APPLICATION_PATH,
            'namespace' => 'Application',
        ));
    $autoloader->addResourceType('MyType', APPLICATION_PATH . '/mytypes');
    return $autoloader;
}

Then you could have a class named Application_MyType_Foo stored in the file application/mytypes/Foo.php.

If you look at the code for Zend_Application_Module_Autoloader, that's essentially what they do to give you autoloading for the models, forms, plugins, etc.


Just create an application specific library. Usually such a class is not the only candidate for a personal library.


One approach is to create a service class (we know where these live: /application/service) that encapsulates the application-specific authentication logic. It can use a library class to do basic auth, e.g. check username and password against a DB, but then also apply application-specific logic before returning the result to the client code.

Example:

class MyApp_Service_User {
  public function authenticate($sUsername, $sPassword) {
    $oAuth = Zend_Auth::getInstance();
    $oAuthAdapter = new Zend_Auth_Adapter_DbTable(...);
    $oResult = $oAuth->authenticate($oAuthAdapter);

    /* Application-specific logic */

    return $oAppSpecificAuthResult;
  }
  ...
}


I recently answered a similar question, have a look at the link below and see if this helps! ;)

Zend autloading different namespaces from the same directory?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜