简体   繁体   中英

Conditionally Include Modules in application.config.php?

With Zend Framework 2, is there a built in means to load certain modules if certain route conditions are met?

Here's a really bad example. Imagine a Login and Registration page were built as separate modules, one would know that the Login module needn't be instantiated with /register is the present route.

If you can look past my horrible example, I think the logic is sound… Some modules provide a breath of listeners and triggers that simply aren't used unless their route is at play. Trying to avoid the bootstrap overhead where possible on a site that serves thousands per minute.

Thought I'd ask around before I roll some means to do it.

Thanks!

Whilst it may be possible to conditionally load modules, it won't be based on a route, as all the application routes live in modules. How would ZF2 know which modules might have routes with 'module loading' conditions attached without loading them first?

The module bootstrap overhead in ZF2 is pretty low. This is an area that was improved considerably over ZF1. It's unlikely that this will be a bottleneck for your application, so don't try and solve a problem unless you know it exists.

Hopefully this isn't too late. I needed to solve a similar problem where I wanted to load the Cli module only when working from cli. This is what I do in my application.config.php file

return call_user_func(function() {

        $modules = array(
            'Application',
        );

        if (\Zend\Console\Console::isConsole()) {
            $modules[] = 'Cli';
        } else {
            $modules[] = 'Api';
            $modules[] = 'Mobile';
        }

        return array(
            'modules' => $modules,
            // Other application config options
        );    
    });

Typically this file would have had "return array(/* ... */);" but by making it into a function, you can apply any logic you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM