We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

What is the most reliable way to get the path to the modules directory?

Development and Programming

Brian Litzinger's avatar
Brian Litzinger
711 posts
16 years ago
Brian Litzinger's avatar Brian Litzinger

I’m working on a module, and I need the full URL/URI to the modules directory. What is the best way to do this? PATH_MOD is kind of what I need, but I need the URL, not the server path. I feel like there is an easy answer to this, and I could write some PHP to get it, but I wanted to see if there is an EE way to get it.

       
Brian Litzinger's avatar
Brian Litzinger
711 posts
16 years ago
Brian Litzinger's avatar Brian Litzinger

I came up with this:

$site_url = $PREFS->ini('site_url') ? $PREFS->ini('site_url') . '/' : '';
        $this->current_directory = str_replace('//', '/', $site_url . $PREFS->ini('system_folder') . '/modules/redeemer/');

But, is site_url ALWAYS defined in every install? It’s been so long since I’ve done a clean out of the box install that I can’t remember. I always have site_url in my config.php file.

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
16 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

I’ve never installed the system directory above root but I know people do it. In those cases I don’t think your method would work.

Why do you need to get the URL path rather than the server path? Are you trying to reference image or stylesheets? If so, you may want to consider putting those in the themes directory and include that instruction in your documentation.

       
Brian Litzinger's avatar
Brian Litzinger
711 posts
16 years ago
Brian Litzinger's avatar Brian Litzinger

I’m doing an ajax call to a file in my module directory, so I need the absolute URL. Relative URL won’t work.

You bring up a good point too, if the entire module directory is below the root an absolute URL won’t work anyway. Crap.

It’s either I move the file to the themes folder, which is kinda dumb because it isn’t a theme, or I re-write 20 lines of PHP in JavaScript.

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
16 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

You wouldn’t want to call directly to the file anyways. If you need an Ajax call it should be to your modules CP area for security. If the function(s) are not within your mcp.module,php file you should use an include() to still use it there. This way you would use the cp URL to your modules page in your Ajax call.

       
The Nerdery's avatar
The Nerdery
16 posts
16 years ago
The Nerdery's avatar The Nerdery

That makes sense. Thanks!

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
16 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

No problem! Post back if you come across challenges with this. I also suggest you run a quick check in your method to make sure the call is an ajax call. One extra (small) layer of security. 😊

       
Brian Litzinger's avatar
Brian Litzinger
711 posts
16 years ago
Brian Litzinger's avatar Brian Litzinger

Yep, already have the is_ajax check going.

       
Bjørn Børresen's avatar
Bjørn Børresen
629 posts
16 years ago
Bjørn Børresen's avatar Bjørn Børresen

Hi,

are you guys talking about an Ajax call from the site (module frontend) or Ajax calls within the backend? (the admin doing some admin stuff in mcp.module.php).

I ask because I’m doing the same myself ..

As for getting the URL this is what I’ve come up with for EE 2.0

$vars['_addon_url'] = str_replace('index.php','',$this->EE->config->item('cp_url')) . "expressionengine/third_party/my_modulename/";

I’m implementing these editable text-areas (jquery) for the admin. So the javascript will call a method in my mcp.module.php file, but what’s the recommended way of giving a response? Normally if you return “test” you will get a full webpage with all HTML .. doing a die(“test”) will work of course, but it’s superhacky 😉

  • bjorn
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.