To change a function parameter in the url with Codeigniter

Asked

Viewed 461 times

0

I wonder if you have the possibility to change a parameter of the url with codeigniter. I have a function created called edit and in it I pass the id of the client to be edited, only that in place of the id that is the parameter, I would like to be shown the name of the client, there is this possibility?

Follow code to analyze:

public function editar($id)
{   
        $data                = array();
        $data['NOMECLIENTE'] = '';
        $data['IDCLIENTE']   = '';

    $cliente = $this->ClienteM->get(array('id_cliente' => $id), TRUE);

    if ($cliente) {
        $data['IDCLIENTE']   = $cliente->id_cliente;
        $data['NOMECLIENTE'] = $cliente->nome_cliente;
    } 

    setURL($data,'cliente');

    $this->parser->parse('painel/cliente_form', $data);
}
  • 1

    Your doubt is not clear. Where does this method come from setURL? You want the name to appear where? In the link or as a link attribute?

  • Hello Shutupmagda the setUrl method and a helper created to define some actions like list and save, I want the name to appear in the url instead of the id parameter

  • as the edit link is being generated?

  • The code you posted does not do what you describe. What this code does is to fill in a array and carry a view on the basis of $id sent via $_GET. It is possible that these links are being generated with setURL, But you don’t show what she does, so it’s hard to help. In addition to showing the function, you should put an example of how the URI is getting and how you want it to look. After all, what is the reason for replacing the id in the URI?

1 answer

0

It is possible to do what you ask: access the given URL an ID but be redirected to another that contains the client name in the URL (I don’t understand what is the advantage of this). What is not possible is to directly access the URL that only contains the name of the client (imagine the case of 2 clients with the same name, which would be accessed? You need to identify them by ID).

What happens is that you will be directed to another control method that will disregard the second parameter, the client name. In a very fast way and ugly, when receiving the $id parameter from the URL, you get the given name, and pass it to a second method, which uses only $id, as the first one, but will also show the name in the URL.

Thus:

public function editar($id) {   
    // Esse 1° método não faz nada, só obtem o nome do cliente e redireciona
    // para o método editar2 (que contém o nome do cliente na URL)
    // e ele sim faz o que precisa fazer
    $id = intval($id); // Sanitize sempre
    $cliente = $this->ClienteM->get(array('id_cliente' => $id), TRUE);

    if ($cliente){
        // Coloquei __CLASS__ aqui por que não sei qual é o nome da classe
        redirect(__CLASS__.'/editar2/'.$id.'/'.$cliente->nome_cliente);    
    } 
    else {
        // Cliente não localizado, tratar...
    }   
}

public function editar2($id, $nome_cliente) {
    // Aqui faz o serviço de verdade, mas agora está exibindo
    // o nome do cliente na URL

    // AQUI ESTÁ A IMPLEMENTAÇÃO VERDADEIRA DO MÉTODO...
}

Browser other questions tagged

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