Custom methods
- Home
- Developing modules
- Admin controllers
- Custom methods
It's also possible to define custom methods which are useable in the admin.
An example would be a tool for a blogs module which allows a given blog post to be shared on social media.
UI methods
Additional methods which are the user-interface of a tool are defined using what's known as an extra method.
The name of the extra method begin with a leading _extra
and is CamelCaps for the rest of the name. Extra methods can have an arbritary number of additional arguments passes into them.
Controllers/Admin/BlogPostAdminController.php
public function _extraShareSocial($blog_post_id) { // UI to render form goes here }
The method returns an array with the title to show and also the html content.
Typically the html content would come from a view. Unlike the main add/edit/delete methods, if a form is required in an extra method then it needs to be included in the returned html.
Controllers/Admin/BlogPostAdminController.php
public function _extraShareSocial($blog_post_id) { $post = Pdb::get('blog_posts', $blog_post_id); $view = new View('modules/Blogs/admin/share_social'); $view->post = $post; return [ 'title' => 'Social share blog post <strong>' . Enc::html($post['title']) . '</strong>', 'content' => $view->render(), ]; }
Note also the bolding around the record name in the returned post title.
The url for an extra method is admin/extra along with the controller slug, and then the lower_case version of the extra method name, and then the argments slash-separated;
admin/extra/<controller-slug>/<method-name>/<arg-one>/<arg-two>/<arg-n>
So the url to share the blog post with id 42 on social networks would be:
admin/extra/blog_post/share_social/42
In this case, the url would probably be added as a edit sub actions link.
Action methods
It's also possible to define methods directly on the controller, and to call these via admin urls.
While it would be tempting to directly create a route for these action methods, by going through the admin controller is more secure as it's guaranteed that admin auth and other security percautions have been met.
Methods can be defined with any name and arguments:
Controllers/Admin/BlogPostAdminController.php
public function shareSocialAction($blog_post_id) { // Do something here }
As the result of these methods is not used, any output would be shown directly without any template or style.
Therefore an action method should end with a Url::redirect
and possibly also a Notification::confirm
with an appropriate message, for example:
Controllers/Admin/BlogPostAdminController.php
public function shareSocialAction($blog_post_id) { $post = Pdb::get('blog_posts', $blog_post_id); // TODO: Share the post on all networks Notification::confirm('Blog post has been shared'); Url::redirect('admin/edit/blog_post/' . $blog_post_id); }
The url for accessing these methods is admin/call along with the controller slug, then the method name as-is, and then the arguments slash-separated;
admin/call/<controller-slug>/<method-name>/<arg-one>/<arg-two>/<arg-n>
So the url which should be included as the action of the social sharing form would be:
admin/call/blog_post/shareSocialAction/42
To finish off this exmaple, here is the admin view you could use for this social sharing:
views/admin/share_social.php
<form action="admin/call/blog_post/shareSocialAction/<?= $post['id']; ?>" method="post"> <?php Form::nextFieldDetails('Message', true); echo Form::text('message'); ?> <p><button type="submit" class="button">Share blog post</button></p> </form>