New route does not work codeigniter

Asked

Viewed 188 times

0

Hello, I created a controller that calls a function with a variable in the index, Mechi in the route works perfectly, only now I created another function within this same controller and it does not work, whenever I call the second function it falls straight into the index. How do I call the second function of this controller? , if you can help me thank.

My controller Home:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {

    public function __construct(){

        parent::__construct();
         $this->load->model('painel_model');
    }

    public function index($meta_link=NULL)
    {  
        if (!$meta_link) {
            redirect('/', 'refresh');
        }
         //informações da pagina
        $data['titulo'] = $produto->nome_produto;
        $data['dados_painel'] = $query;     
        $data['view'] = 'painel/software/info';
        $data['produto'] = $produto;
        $data['fotos'] = $this->painel_model->getFotos($produto->id);

         //carrego template
         $this->load->view('painel/software/template', $data);  
    }

    public function checkout()
    {  
        //Dados da loja
        $query = $this->painel_model->getDadosPainel();

        //menu topo
        $data['categorias'] = $this->painel_model->getCategorias();

         //informações da pagina
          $data['titulo'] = 'Checkout';
          $data['dados_painel'] = $query;       
          $data['view'] = 'painel/checkout/baixar';

         //carrego template
         $this->load->view('painel/checkout/template', $data);  
    }

}

My Routes:

//toras pagina home
$route['home/(:any)'] = 'home/index/$1';

If I call the Function index, it works perfect, now if I call the checkout it doesn’t work, it keeps returning the index, how do I call the checkout by route? If you can help me, thank you.

1 answer

1


The reason for unexpected behavior

The behavior of only returning the index method view of your Home controller, i.e., calling only the index method, occurs because you have defined a single path in the file application/config/routes.php which it possesses, after the /, a wildcard type (:any) which will map Uris to the following pattern...

  • your domain.com/home
  • your domain.com/home/checkout
  • your domain.com/home/anyone else

at all times for your Home controller index method.

To better understand the functioning of the routes in the framework can access official documentation.

The solution

For you to be able to call the checkout method, simply modify the existing route to solve only for the index method of the Home controller and add a new route for the checkout method. Then you only need to modify your route file which, according to the context presented in the question, would look like this:

//toras pagina home
$route['home'] = 'home/index/$1';
$route['home/checkout'] = 'home/checkout';

I hope I’ve helped.

  • I made some logical changes worked with your tips. Valew friend

Browser other questions tagged

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