Jump to content

module gets auto-installed if called from front-end?


applab
 Share

Recommended Posts

Hi All,

I'm making my first serious foray into module development and I've discovered some behaviour which I found surprising...

If I uninstall my module from the backend but do not delete the file, then I refresh a front-end page that is using that module, I would expect to see some kind of error but what happens is the module gets automatically re-installed.

I can mitigate this behaviour by wrapping my front-end code in:

if(modules()->isInstalled('MyModule')) { ... }

but that doesn't seem like the best approach if the module is used in multiple places.

Is there something I can add to my modules install() method to prevent it getting auto-installed in this situation?

Thanks.

Link to comment
Share on other sites

Hi @applab Probably somewhre in your code you are calling $modules->get("MyModule"). 

So just check before getting this module. 

if ($modules->isInstalled("MyModule")) {
    echo $modules->get("MyModule")->renderBlock();
}

Also if this module is required by another module it will be installed (Not sure, but looks like it behaves in this way).

Link to comment
Share on other sites

Hi @Zeka, 

Yes, I am using $modules->get("MyModule") in my front-end template.  I was just surprised to see MyModule get installed as a result of that code rather than throwing an error, which is what I was expecting. 

I've achieved my original goal of preventing the install in that situation but I've realised that I still need to use isInstalled() in my front-end templates to avoid runtime errors so, thanks.

Link to comment
Share on other sites

5 hours ago, applab said:

I was just surprised to see MyModule get installed as a result of that code rather than throwing an error, which is what I was expecting. 

That is the default behaviour.

Quote
  • If the module is not installed, but is installable, it will be installed, instantiated, and initialized. If you don't want that behavior, call $modules->isInstalled('ModuleName') as a conditional first.

Alternatively, you can use the method $modules->getModule() with noInstall option.

<?php namespace ProcessWire;

$modules->getModule('MyModule', ['noInstall' => true]);
Quote
  • noInstall (bool): Specify true to prevent a non-installed module from installing from this request. (default=false)

 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...