How to have presenter method without template
- kevin.waterson@gmail.com
- Member | 81
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?
- Kamil Valenta
- Member | 820
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)
- Marek Bartoš
- Nette Blogger | 1275
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)