getherbert / herbert

The WordPress Plugin Framework:
http://getherbert.com/
631 stars 94 forks source link

How do you Process a POST Request on a Panel? #49

Open kkeiper1103 opened 9 years ago

kkeiper1103 commented 9 years ago

I'm using the panels feature like this:

$panel->add([
    'type' => 'panel',
    'as' => 'productImporterPanel',
    'title' => 'Product Importer',
    'slug' => 'products-importer',
    'icon' => 'dashicons-download',
    'uses' => __NAMESPACE__ . "\\Controllers\\ImportController@getIndex"
]);

The method exposes a form for uploading a spreadsheet that will be imported into the database. How do I register a method to handle a POST request to that panel?

If I'm doing it wrong, please tell me what the official way to do this is.

jasonagnew commented 9 years ago

Hi @kkeiper1103

You can add extra requests like:

    'get' => [
        'edit' => __NAMESPACE__ . '\\Controllers\\SetController@show',
        'delete' => __NAMESPACE__ . '\\Controllers\\SetController@getDestroy',
    ],
    'post' => [
        'uses' => __NAMESPACE__ . '\\Controllers\\SetsController@store',
        'edit' => __NAMESPACE__ . '\\Controllers\\SetController@update',
        'delete' => __NAMESPACE__ . '\\Controllers\\SetController@postDestroy',
    ]

or

    'post.add' => __NAMESPACE__.'\Controllers\AdminController@add',
    'post.delete' => __NAMESPACE__.'\Controllers\AdminController@delete',

post.uses is the standard post request, if you want to set an action &action=update then you would use post.update. If you want set fake a method, for example trigger a post panel using get you can add &_method=POST.

Check out the example plugin: https://github.com/getherbert/example-plugin/blob/master/app/panels.php or: https://github.com/bigbitecreative/wordpress-socializr/blob/master/bootstrap/panels.php

kkeiper1103 commented 9 years ago

Hey, Awesome! Works like a charm; I'll be sure to check out the example plugin too.

On Thu, Jul 9, 2015 at 8:43 AM, Jason Agnew notifications@github.com wrote:

Hi @kkeiper1103 https://github.com/kkeiper1103

You can add extra requests like:

'get' => [        'edit' => __NAMESPACE__ . '\\Controllers\\SetController@show',        'delete' => __NAMESPACE__ . '\\Controllers\\SetController@getDestroy',    ],    'post' => [        'uses' => __NAMESPACE__ . '\\Controllers\\SetsController@store',        'edit' => __NAMESPACE__ . '\\Controllers\\SetController@update',        'delete' => __NAMESPACE__ . '\\Controllers\\SetController@postDestroy',    ]or    'post.add' => __NAMESPACE__.'\Controllers\AdminController@add',    'post.delete' => __NAMESPACE__.'\Controllers\AdminController@delete',

post.uses is the standard post request, if you want to set an action &action=update then you would use post.update. If you want set fake a method, for example trigger a post panel using get you can add &_method=POST.

Check out the example plugin: https://github.com/getherbert/example-plugin/blob/master/app/panels.php or: https://github.com/bigbitecreative/wordpress-socializr/blob/master/bootstrap/panels.php

— Reply to this email directly or view it on GitHub https://github.com/getherbert/herbert/issues/49#issuecomment-119949023.