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
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.
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_]
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
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().
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.
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
I tried something similar: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.BASE.AMP.'C=addons_accessories'.AMP.'M=process_request'.AMP.'accessory=mailinglist_acc'.AMP.'method=process_export'
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'Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.