How to have presenter method without template

16 days ago

+
0
-

I have a simple delete function in a presenter.

<?php
        public function renderDeleteCategory( $id )
        {
                try {
                        $this->flashMessage("Recipe Category Deleted", 'success');
                        $this->db->table('recipe_category')->where('id', $id)->delete();
                        $this->redirect('Admin:Category');

                } catch (Nette\Security\AuthenticationException $e) {
                        $form->addError('Delete Recipe Category Failed'.$e->getMessage() );
                }
        }
?>

This works fine, but it requires a matching template, named deletecategory.latte.
I have created a blank template, and that works, but does not seem the right way to do things.
How do I have this method, without the template?

16 days ago

kamil_v
Member | 107
+
+1
-
public function handleDeleteCategory( $id )
        {
                try {
                        $this->flashMessage("Recipe Category Deleted", 'success');
                        $this->db->table('recipe_category')->where('id', $id)->delete();
                        $this->redirect('Admin:Category');

                } catch (Nette\Security\AuthenticationException $e) {
                        $form->addError('Delete Recipe Category Failed'.$e->getMessage() );
                }
        }

or

public function actionDeleteCategory( $id )
        {
                try {
                        $this->flashMessage("Recipe Category Deleted", 'success');
                        $this->db->table('recipe_category')->where('id', $id)->delete();
                        $this->redirect('Admin:Category');

                } catch (Nette\Security\AuthenticationException $e) {
                        $form->addError('Delete Recipe Category Failed'.$e->getMessage() );
                }
		$this->terminate();
        }

(second one is too bad practice)

Be careful:

  • redirect in render method
  • redirect in try block

Last edited by kamil_v (2020-01-05 14:24)

16 days ago

CZechBoY
Member | 3452
+
0
-

Action + redirection looks best for me.

16 days ago

Mabar
Member | 201
+
+1
-

For delete use form with single button, if you use nette 2.4 or lower.
For nette 3.0 use handle method, they have built-in CSRF protection

Btw. handle method is called after action, be careful
And try to not put redirect inside try-catch. Too often happened to me, that redirect exception was catched

Last edited by Mabar (2020-01-05 23:51)

16 days ago

dkorpar
Backer | 79
+
0
-

If some action is deleting/updating/inserting data that allways HAVE to be post request,
otherwise you're allways risking another reques on (multiple) back buttons/refresh.

Last edited by dkorpar (2020-01-06 10:36)