views:

39

answers:

2

I want to set up include paths (and other paths, like view script paths) based on the module being accessed. Is this safe? If not, how could I safely set up include paths dynamically? I'm doing something like the code below (this is from a controller plugin.)

public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {

    $modName = $request->getModuleName();
    $modulePath = APP_PATH.'/modules/'.$modName.'/classes';
    set_include_path(get_include_path().PATH_SEPARATOR.$modulePath);

}
A: 

I'm not sure whether it is safe or not, but it doesn't sound like the best practice. What if someone entered a module name like ../admin/? You should sanitize the module name before using it.

amdfan
A: 

It's fine as long as you sanitize your variables before using them, but it won't be very performant. Fiddling with include paths at runtime causes a serious impact performance.

You're trying to load models/helpers per module? You should look at Zend_Application:

Zend_Application provides a bootstrapping facility for applications which provides reusable resources, common- and module-based bootstrap classes and dependency checking. It also takes care of setting up the PHP environment and introduces autoloading by default.

Emphasis by me

Luiz Damim
That's a good idea, but my file structure isn't standard and it's going to take a bit of work to extend the module bootstrap to suit my needs. Doesn't dispatchLoopStartup() happen before any action controllers are loaded? I could add it to routeShutdown() instead. I'm not sure when the runtime begins...
Robin Canaday
What I mean is, Zend uses set_include_path() in most of the examples, and the bootstrapping classes/resources will definitely set the paths after the scripts start.
Robin Canaday