After doing Insert, redirect to the list of records in Kohana

Asked

Viewed 179 times

2

I started working with Kohana yesterday and I got a problem here.

My default Controller is the "page" and the action is "home". In the "page" Controller I have these functions here to mount the layout:

class Controller_Page extends Controller_Index {

 public function action_home(){
     $this->template->title = "Helpdesk";
     $this->template->content = View::factory('home');
 }
 public function action_newClient(){
     $this->template->title = "Helpdesk";
     $this->template->content = View::factory('newClient');
 }
 public function action_listClient(){
     $this->template->title = "Helpdesk";
     $this->template->content = View::factory('listClient');
 }
}

I also have a Controller called "client" and within it these functions:

class Controller_Client extends Controller_Template {
//put your code here
public function action_novo(){
    $cliente = ORM::factory('cliente');        
    $cliente->nomeCliente = $this->request->post('nomeCliente');
    $cliente->cnpjCliente = $this->request->post('cnpjCliente');
    $cliente->dataCadastro = date("Y-m-d H:i:s");
    if($cliente->save()){
        $session = Session::instance();
        $session->set('msg', 'Cliente cadastrado com sucesso!');
        $this->redirect('page');            
    }

}
public function action_lista(){
    $cliente = ORM::factory('cliente')->find_all();
    $view = View::factory('listClient');
    $view->set('clientes',$cliente);
    $this->response->body($view);
}
}

In the template you are mounting, I have a link that points to a form, which in this case is the function action_newClient of controller page where a customer registers, it works normal. In this form, in the action I am pointing it to the method action_novo of controller Client.

How can I redirect to a screen that shows all results?

See that I made the method action_lista in the controller Client, but I don’t know what to call this method in view that will list the data.

  • Let me get this straight. You want to know how to consume customer data in the view?

  • More or less that, in fact I even managed to do what I wanted and my doubt has now changed It would be as I call a method of an external control within another controller

  • Fine, don’t do it. Don’t call one method a controller on another, that’s bad practice. What I would recommend is to redirect to another controller, or extract the common code to your own class.

1 answer

1

From what I understand you searched in the database all the data of the table 'client' and there on

$view->set('customers',$customer);

Now it’s your var in the view to show the Infos is the 'clients' you don’t actually call the method because you are already in the corresponding view.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.