getherbert / herbert

The WordPress Plugin Framework:
http://getherbert.com/
634 stars 95 forks source link

Admin GET routes without making panels #177

Closed angelocala94 closed 7 years ago

angelocala94 commented 7 years ago

There is a way to add a GET page, for example for editing an element, without making another panel? At the moment i noticed that routes.php can be used for making pages external to wp-admin, but if I want to make a page inside the admin area?

Can I do that? Because my view needs an ID of an element, so I can't make a panel for that.

Thanks.

ghost commented 7 years ago

You can add a get variable after the url string...

<a href="?page=my_panel&id={{ post.ID }}">Edit</a>

~/wp-admin/admin.php?page=my_panel&id=6

if(isset($id)){
     //do something else
}

Or, if you would like something more eloquent, you can populate the edit url from the model which appends the GET parameter to the panel url automatically.

In your panel definition, reference a name which can be accessed using the panel_url method in the Helper:

$panel->add([
    'type'   => 'panel',
    'as'     => 'editor', //used by panel_url() method
    'title'  => 'Edit',
    'slug'   => __NAMESPACE__.'_editor',
    'uses' => __NAMESPACE__.'\Controllers\MyController@edit'
]);

In your Model add an ancestor attribute that uses the Helper method to generate the panel url and append the the ID attribute (or any others) from the model using $this-> in object context:

public function getRouteEditAttribute()
{
        return panel_url('MyNamespace::editor',[
            'id'=>$this->ID
        ]);
}

And in your view simply reference the new ancestor:

<a href="{{ post.route_edit}}">{{ post.post_title }}</a>

angelocala94 commented 7 years ago

I thought there was another way since the documentation is not complete, thank you!