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

Executing a PHP script within the EE/CI framework so as to have the methods/variables available

Development and Programming

Carl W Crawley's avatar
Carl W Crawley
136 posts
15 years ago
Carl W Crawley's avatar Carl W Crawley

Hi All,

This is probably going to be a fairly straightforward post/request, I’ve trawled the documentation and forums to no avail.

I’ve developing an accessory at the moment which is almost complete. What I now need to be able to do to complete the work is when the user clicks a link, it executes a php script which does some wondrous stuff in the database.

I’ve created the php file in my third_party/{accessory}/php/script.php - however, current when I execute it (currently via jQuery $.load() function), the $this->EE =& get_instance(); obviously isn’t available to me.

How do I execute the script within the CI/EE framework so that I have access to all the methods/functions?

Hopefully I’ve explained myself properly on this.

Regards,

Carl

       
Rob Sanchez's avatar
Rob Sanchez
335 posts
15 years ago
Rob Sanchez's avatar Rob Sanchez

You should add a module to your addon. You can add a mod file with just a class definition and no methods, since you don’t really need a front end tag. Your upd file should be pretty straightforward. The mcp file is where the action happens: http://ellislab.com/expressionengine/user-guide/development/modules.html#cp_urls You can add methods there and execute them from the CP via ajax. Plus, you could use views if you want.

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
15 years ago
Pascal Kriete's avatar Pascal Kriete
I’ve developing an accessory at the moment which is almost complete.

So the link they’ll be clicking is in the CP? Or do you want a link on the frontend?

       
Carl W Crawley's avatar
Carl W Crawley
136 posts
15 years ago
Carl W Crawley's avatar Carl W Crawley

The link is in the CP - there is no front end for the accessory

I think Rob’s post has given me the information I need - I’ll have a read up and post further if I have any difficulties.

Cheers,

Carl.

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
15 years ago
Pascal Kriete's avatar Pascal Kriete

You don’t need a module to process the link, a link to addons_accessories->process_request can be used to access an accessory method directly.

So this url:

BASE.AMP.'C=addons_accessories'.AMP.'M=process_request'.AMP.'accessory=test_acc'.AMP.'method=process_ajax'

Would call the process_ajax method in the test_acc accessory.

However if you’re adding a db table you will want a module as Rob pointed out. And it gives a bit more flexibility for additions down the road as well.

[Edit: Whoops, method name needs to start with process_]

       
Carl W Crawley's avatar
Carl W Crawley
136 posts
15 years ago
Carl W Crawley's avatar Carl W Crawley

oooo - even better! perfect, thanks Pascal!

       
Rob Sanchez's avatar
Rob Sanchez
335 posts
15 years ago
Rob Sanchez's avatar Rob Sanchez

I had no idea about accessories and process_request. Super awesome. I’m not sure if that’s in the docs anywhere.

       
Carl W Crawley's avatar
Carl W Crawley
136 posts
15 years ago
Carl W Crawley's avatar Carl W Crawley

Am I able to still use the $_GET[”] to grab querystring items? If not, whats the recommended way to pass information into the function?

       
Carl W Crawley's avatar
Carl W Crawley
136 posts
15 years ago
Carl W Crawley's avatar Carl W Crawley

I think I found it…. $this->EE->input->get(‘variable’,true); seems to work.

       
Carl W Crawley's avatar
Carl W Crawley
136 posts
15 years ago
Carl W Crawley's avatar Carl W Crawley

Pascal - one more thing, if I decide against going down the AJAX route and just link to the addon, what do I need to return to get it to display the page after it’s executed? I’m assuming some kind of return statement?

Cheers,

C.

       
Carl W Crawley's avatar
Carl W Crawley
136 posts
15 years ago
Carl W Crawley's avatar Carl W Crawley

The suggested route has worked perfectly for me - but I have had to make some tweaks to the accessory and as a result am not executing it via jQuery any more. How do I get the function to return back to the page it was on once it’s processed the process function?

Regards,

Carl

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
15 years ago
Pascal Kriete's avatar Pascal Kriete

Sorry, forgot to subscribe to the thread.

You’ll need to do a redirect, since it’s a new request. it’s a little tricky as there are pages in the CP that you cannot directly return to (anything that requires POST data).

There’s a method in the cp library called get_safe_refresh(). You’d need to add that result to your link, grab it on the processing page and then pass it to functions->redirect(). But it brings up a good point for potential improvements. Might add some logic to the process_request method to make that easier in the future.

And yep, $_GET still works, as does input->get() and input->get_post().

       
Carl W Crawley's avatar
Carl W Crawley
136 posts
15 years ago
Carl W Crawley's avatar Carl W Crawley

Thanks for the reply Pascal.

Interesting. A redirect sounds sensible enough to be honest - so I’ll investigate that.

It does bring up an interesting potential for an improvement, adding logic to the process method would be very useful indeed.

Thanks again - I hope you don’t mind all the 20 questions, I’ve written many addons/extensions/plugins in the past, but none that require posting within the CP, so this is all new to me and an awesome learning curve!

Cheers,

Carl.

       
moonbeetle's avatar
moonbeetle
81 posts
15 years ago
moonbeetle's avatar moonbeetle

I tried something similar:

BASE.AMP.'C=addons_accessories'.AMP.'M=process_request'.AMP.'accessory=mailinglist_acc'.AMP.'method=process_export'

And I get: Fatal error: Class name must be a valid object or a string in /var/www/html/yourrenamedsystem/expressionengine/controllers/cp/addons_accessories.php on line 243

       
Jamie Rumbelow's avatar
Jamie Rumbelow
546 posts
15 years ago
Jamie Rumbelow's avatar Jamie Rumbelow
I tried something similar:
BASE.AMP.'C=addons_accessories'.AMP.'M=process_request'.AMP.'accessory=mailinglist_acc'.AMP.'method=process_export'
And I get: Fatal error: Class name must be a valid object or a string in /var/www/html/yourrenamedsystem/expressionengine/controllers/cp/addons_accessories.php on line 243 Though my class name, constructor and method names seem OK to me.

You don’t need the _acc suffix on the URL - just use accessory=mailinglist!

BASE.AMP.'C=addons_accessories'.AMP.'M=process_request'.AMP.'accessory=mailinglist'.AMP.'method=process_export'
       
1 2

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.