Skip to content
Advertisement

Submit a Form to Custom Route in osclass

I am making an osclass plugin. I have added a page for my plugin in the admin section of the site. I have also created a route for my plugin.

What I would like to do is create a form on my plugin’s page in the admin. The action attribute of this form will be the base url of my custom route. The form will then add a query string of parameters to the base url when the administrator submits it. Specifically, the query string should include an id parameter, for example ?id=1.

Here is what I have:

JavaScript

I have tested this form. Instead of dropping me off at my custom route url, osclass redirects me to the url of the admin dashboard (with the correct id included in the query string along with an octoken). I need it to trigger my custom route.

I have tested the custom route by printing a link to it on the same page where the form is. When accessed by clicking the link, the custom route works fine.

Thanks to anyone who can help!

Advertisement

Answer

I did eventually figure this out, or at least one way to do it. You can use the renderplugin_controller hook. This hook is fired anytime a user requests the url to a custom route in the admin area. To add a callback called controller to it, I added a line in the constructor of my plugin’s bootstrap class:

JavaScript

Then, the hooked controller method checks for a route parameter in the url when the renderplugin_controller hook is run. If the route parameter exists, and it belongs to my plugin, I have a separate controller class that gets instantiated and processes the request from this point forward. I can test if the route belongs to my plugin by matching the route name to a prefix specific to my plugin.

JavaScript

As far as the Admin_Controller class goes, it should extend the AdminSecBaseModel class, according to the examples I’ve seen. The basic structure for it is:

JavaScript

If you want another more complete example of how to use the renderplugin_controller hook to handle a form submission to a custom route, I recommend checking out this repository (it’s for the osclass user custom fields plugin). I basically copied what was done there. In particular, the UserCfAdmin class and the UserCfController_Admin class showed me exactly how to do this.

You can find another guide to making a controller for your custom routes here.

Note: I am using osclass v3.8 on the site i was making this for.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement