Menus Routing


In WordPress as well as WP Bones architecture, the routing is quite different from Laravel. Instead of a true routing system, there is menu routing. However, you can still handle basic HTTP verbs such as GET, POST, PUT, PATCH, and DELETE. In the example above, we used the get verb in the route key. If your view contains a form with a POST method, you can use:

...
  'route' => [
    'get'  => 'Dashboard\DashboardController@secondMenu',
    'post' => 'Dashboard\DashboardController@updateValues'
  ]
...  

In this case, when an HTTP POST request is made, the method updateValues in Dashboard\DashboardController will be called.

Form Method Spoofing

HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH orDELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used in the HTTP request:

<form action="" method="POST">
    <input type="hidden" name="_method" value="PUT">
</form>

Resource

Alternatively, instead of specifying each single HTTP verb, you may use the resource key in order to call the default method in your view controller:

...
  'route' => [ 'resource' => 'Dashboard\DashboardController' ],
...

Once you have assigned the view controller to a given route resource, you may use the standard methods instead.

class DashboardResourceController extends Controller {
  
  // GET
  public function index() {
    //
  }
  
  // POST
  public function store() {
    //
  }
  
  // PUT AND PATCH
  public function update() {
    //
  }
 
  // DELETE
  public function destroy() {
    //
  }
}

You can use the php bones command to create a controller:

php bones make:controller MyController

Create the folder and namespace

php bones make:controller Billing/Market/MyController

Redirect in load

When a submenu is clicked, your controller’s methods are executed inside the admin view. This means that the HTTP headers are already sent. So, you can’t execute any HTTP redirect but only a script - workaround - redirect. Anyway, WP Bones provides a redirect method that you may use. If the headers are already sent, a JavaScript script redirect will be done:

// POST
public function store() {
  // your store command
  $this->redirect( 'some url' );
}

Also, WP Bones provides a special route called load. It is executed before the page is rendered:

...
  'route' => [ 'load' => 'Dashboard\DashboardController@load' ],
...
đź’ˇ

You will need to use this method to add meta boxes.

public function load()
{
    wp_enqueue_script( 'common' );
    wp_enqueue_script( 'wp-lists' );
    wp_enqueue_script( 'postbox' );
    
    // TODO: add your meta boxes
}

This route will be called on the load-{hook} action. Here you can use wp_redirect() or the WP Bones redirect method. Let’s see an example:

<form action="" method="post">
  <input type="hidden" name="_redirect" value="http://undolog.com">
  <button class="button">POST with redirect</button>
</form>

In the load method of the controller:

public function load()
{
  if( $this->request->get( '_redirect' ) ) {
    // the wp_redirect() will be done
    $this->redirect( $this->request->get( '_redirect' ) );
  }
}