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:

<form action="<?php echo osc_route_admin_url('my-custom-route'); ?>" method="get">
  <h2>User Profile form</h2>
  <select name="id">
    //...options, each one's value is a registered user
  </select>
  <input type="submit" value="Submit">
</form>

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:

function __construct() {
  //...
  osc_add_hook( 'renderplugin_controller', array($this, 'controller'));`
  //...
}

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.

 public function controller(){
      
   if (is_null(Params::getParam('route'))) return;

   //strict type checking
   if ( false !== strpos( Params::getParam('route'), 'my-prefix' )){
     $controller = new Admin_Controller();
     $controller->doModel();
   }
}

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:

class Admin_Controller extends AdminSecBaseModel {

  //property to hold my model class
  public mUser;

  public function __construct(){
    parent::__construct();
    //instantiate my model class
    $this->mUser = User::newInstance();
  }

  public function doModel(){
 
   /*
   I only have one route in my plugin.
   If you had multiple you could switch over them
   with switch(Params::getParam('route'))
   */

   $id = Params::getParam('id');
   $user = $this->mUser->findByPrimaryKey( $id );
   //do whatever you want with the user object
  }
}

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