Jump to content

best way to get module's path relative to root?


bernhard
 Share

Recommended Posts

Hey!

Something that has annoyed me several times already over time. I have this line in one of my modules:

config()->scripts->add(config()->urls->siteModules . 'RockCRM/scripts/createInvoiceButton.js');

The line of code is placed in one module inside folder /site/modules/RockCRM, so what I tried first was

config()->scripts->add(__DIR__ . '/scripts/createInvoiceButton.js');

But this throws an error that it is not allowed to add resources like this. I really want to get rid of the 'config()->urls->siteModules . 'RockCRM'' part! Is there any native function to add scripts via path (not via relative url)? Or is there a way to get the relative url of a module? I don't want to add the filename or module's name manually - hey, I'm already in that file, so that should really be easier, quicker and more future proof (thing of renaming the module one day...).

I've built some helper functions in the past to do that, but they are spread wildly and implemented differently. I want to get one standard approach. I thought of submitting a PR to make the scripts take absolute paths as well, but I only want to do that if you guys don't know of an existing and good solution.

Thanks for your help!

Link to comment
Share on other sites

On 9/27/2018 at 3:27 PM, Soma said:

config()->scripts->add(config()->urls->{$this->className} . 'scripts/createInvoiceButton.js');

great! thx soma! this is also possible and imho the cleanest solution:

config()->scripts->add(config()->urls($this) . 'scripts/test.js');

thx ? 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

6 hours ago, adrian said:

 

You don't even need className 


config()->urls->$this . 'scripts/test.js';

 

Just want to note that in case of multi-instance usage

 $this->wire("config")->paths->templates != config()->paths->templates

So in modules developement you should avoid using function API.

  • Like 3
Link to comment
Share on other sites

7 hours ago, Soma said:

So in modules developement you should avoid using function API.

I completely agree. The functions API may not even be turned on for the user's PW install.

That said, you could still use:

$this->wire('config')->urs->$this

as the safest short URL.

  • Like 1
Link to comment
Share on other sites

Thank you for the discussion, really appreciate it! What about this?

urls($this); // using functions api
$this->urls($this); // without functions api and multi-instance proof

I guess this is about as short as it can get ?

The initial example would get:

// from this
config()->scripts->add(config()->urls->siteModules . 'RockCRM/scripts/createInvoiceButton.js');
// to that
config()->scripts->add(urls($this) . 'scripts/createInvoiceButton.js');

 

23 hours ago, Soma said:

Just want to note that in case of multi-instance usage

Thank you soma for that hint. That's a real drawback, even if I've never worked with multi-instance so far. But I think it would be the best to already take care of that in my daily work. Unfortunately I totally lose intellisense in my IDE if i use $this->urls() instead of urls() ? Does anybody know if I can do something about that? Then it would be perfect ? 

Link to comment
Share on other sites

Ok... I did some more testing and I think it's better not to use the functions api at all in module development. It's a level of lazyness that is not good IMHO and could lead to severe problems/drawbacks in the future (when using the module in a multi instance environment). Thank you @Soma for that veto!

5 hours ago, bernhard said:

Unfortunately I totally lose intellisense in my IDE if i use $this->urls() instead of urls() ?

That statement was actually wrong! I get proper suggestions when I stick to proper syntax and don't use shortcuts. I think that is a good practise as it is more verbose to newcomers (I have always liked reading other's code and how easy to understand PW's api is) and also reflects the internal PW structure better than plain functioncalls (wordpress is calling...).

3WFJXhU.png

So my (currently ? ) final version is:

$this->config->scripts->add($this->config->urls($this) . 'scripts/createInvoiceButton.js');

 

Link to comment
Share on other sites

For module development you should actually use:

$this->wire('config')->scripts->add($this->wire('config')->urls->$this

Take a look at Ryan's Github repo (https://github.com/ryancramerdesign/) for his most recent modules and you'll see that is the approach he takes.

@LostKobrakai has an excellent post on when it's ok use: $config vs $this->config vs $this->wire('config') but I can't find it right now.

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

4 minutes ago, bernhard said:

I'd prefer $this->wire->scripts instead of $this->wire('scripts') - would that also be ok?

I think so, although typically I see:

$this->wire()->scripts rather than $this->wire->scripts

although I don't honestly know if it makes a difference.

image.png.928a68cfcf33632443e2623d122e86ca.png

OT, but note that in the Console, I need to use $module instead of $this for the name of the module, but otherwise you can test all these options there.

  • Like 1
Link to comment
Share on other sites

Besides the multi-instance thing, Ryan brought to my attention recently another reason to prefer $this->wire('config') or $this->wire()->config over $this->config...

If you are doing this inside a class (e.g. a module) then it's less efficient to do $this->config because PW has to first check to see if you have a property or method in the class named config, and only after that go to the $config API variable. With $this->wire('config') or $this->wire()->config there is no uncertainty.

Personally I prefer $this->wire()->config over $this->wire('config'), only because it's easier to paste it in or do a find/replace when updating older code, and easier to assign a shortcut to $this->wire()-> in my IDE and have my cursor end up in the right place after I've typed the API variable name. ?

 

  • Like 2
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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