Jump to content

Custom namespace in modules


Recommended Posts

Hi there,

I just tried to implement a process module for a PW3 (3.0.32 devns) installation with a custom namespace and found that there's an issue with assigning the new Process to the newly created admin page.

( ! ) Warning: class_parents(): Class ProcessWire\ProcessNamespaceTest does not exist and could not be loaded in /var/www/public/pw3/wire/core/Functions.php on line 721

It looks like the custom namespace is ignored...

The demo module looks like this:

<?php namespace ACME;

use ProcessWire\Process;

/**
 * ProcessNamespaceTest/ProcessNamespaceTest.module
 */
class ProcessNamespaceTest extends Process
{
  ...

I also added the appropriate settings for autoloading in the composer.json file and did a composer dump-autoload:

"autoload": {
  "files": [
    "wire/core/ProcessWire.php"
  ],
  "psr-4": {
    "ACME\\": [
      "site/modules/ProcessNamespaceTest"
    ]
  }
}

 

Does anyone have an idea if I missed something?

 

Thanks

Alex

Link to comment
Share on other sites

21 minutes ago, netcarver said:

I think you might have missed a leading '\' on the 'Processwire' name. Try this...


use \ProcessWire\Process;

Not 100%  sure though.

Thanks for that hint, but the warning still persists...

It's also worth to mention that everything works fine if the namespace is initially set to ProcessWire. If the namespace is changed back to ACME later, the module still works without problems. It's only the page editor for the admin page where the warning occurs.

Link to comment
Share on other sites

Jumplinks 2 (in development) makes use of namespaces. I simply require a bootstrap file (say, bootstrap.php) at the top of the module's main file, and this file uses the PW classloader to add the namespace, like so:

$this->classLoader->addNamespace('Jumplinks', __DIR__ . '/src/Jumplinks');

As such, you do not need to use the composer.json file for this.

Link to comment
Share on other sites

Hi Mike,

thanks for your answer! I once tried to use PW's build in class loader but wasn't sure where to call addNamespace...

What do you mean by 

On 29.8.2016 at 6:08 PM, Mike Rockett said:

at the top of the module's main file

If the main module file resides directly in the module's folder, it should be like this?

<?php namespace ACME;

$this->classLoader->addNamespace('ACME', __DIR__);

use ProcessWire\Process;

/**
 * ProcessNamespaceTest/ProcessNamespaceTest.module
 */
class ProcessNamespaceTest extends Process
{
  ...

This is not working... I'm also not sure what $this refers to in this context ???

Link to comment
Share on other sites

  • 3 years later...

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

×
×
  • Create New...