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

Is there a "site_url" constant for use in module code?

Development and Programming

R.K.Foster's avatar
R.K.Foster
138 posts
13 years ago
R.K.Foster's avatar R.K.Foster

Greetings,

I’m new to module development so maybe this is obvious to the more experienced. I want to simply use the main site url as a default value in a module if nothing else is selected. In an Exp Eng template it would just be site_url in curly brackets. There is this constant reference here… http://ellislab.com/expressionengine/user-guide/development/constants.html

But strangely it has constants for seemingly everything BUT the site url.

Is there a way to do this?

Thanks, Bob.

       
Bhashkar Yadav's avatar
Bhashkar Yadav
727 posts
13 years ago
Bhashkar Yadav's avatar Bhashkar Yadav

Hi Bob,

In the module, you can use following code to get the site url as:

$site_url = $this->EE->config->item('site_url');

or simply assign this value within constructor and access it anywhere within the module like $this->site_url;

Best Regards,

       
Bransin's avatar
Bransin
157 posts
13 years ago
Bransin's avatar Bransin

Yes, Bhashkar is right.

A lot of the time I find myself switching back and forth between Codeigniter Docs and EE Docs.

See heading “Fetching Config Items” on this page: http://ellislab.com/codeigniter/user-guide/libraries/config.html

Codeigniter will be $this->config->item, where EE will always be $this->EE->config->item

       
Bhashkar Yadav's avatar
Bhashkar Yadav
727 posts
13 years ago
Bhashkar Yadav's avatar Bhashkar Yadav

In the Expression Engine, there are some predefined variables like EXT, BASEPATH, APPPATH, FCPATH which can be used within any add-on but not SITE_URL.

So it need to fetch it from config variables via the code.

Best Regards,

       
Bransin's avatar
Bransin
157 posts
13 years ago
Bransin's avatar Bransin

Furthermore, I think it’s defined in the config for Multiple Site Manager reasons and it wouldn’t make sense to make it a Constant.

       
R.K.Foster's avatar
R.K.Foster
138 posts
13 years ago
R.K.Foster's avatar R.K.Foster

Thanks Bhashkar. That was what I was looking for. Also, Bransin, the lack of a constant for compatibility with Multiple Site Manager makes complete sense to me.

I tried adding the line of code to the constructor function but I must be doing something wrong with the coding. This leads me to another question about how best to create “global” values within Exp Eng modules for use by any function in the module. Following is the construct function…

/**
  * FUNCTION - Construct
  */
 function __construct()
 {

  $this->EE =& get_instance() ;
  $site_url = $this->EE->config->item('site_url') ;

 } // end function construct

However, if I use this->$site_url or this->EE->$site_url something goes wrong with the code. I tried the following…

$site_url = $this->EE->site_url ;
$site_url = $this->site_url ;

Both of the above return an empty value.

Thanks, Bob.

       

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.