codeigniter 3 only default url works

Asked

Viewed 91 times

0

I have a program made in PHP with codeIgniter. In my controler I have two methods, put in the file Routes.php so $route['default_controller'] = 'carta';, calls the index() function. However $route['default_controller'] = 'carta/elemento';, it calls the element() method. However, I want to call the element() method not as default, but by url. I have tried $route['elementos'] = 'carta/elementos';, but when I try to access returns to the home page.

Controller: `

    public function __construct() {

        parent::__construct ();
        $this->load->helper("url");
        $this->load->model ( "carta_model" );

      }

    public function index() {
        $dados = array("nome" => "Hugo") ;
        $this->load->view("cartas/carta_view.php", array("dados" => $dados));


    }

    public function elementos() {
        $elementos = $this->carta_model->getElementos();
        $this->load->view("cartas/carta_view.php", array("elementos" => $elementos));

    }
} ?>` 

Routes.php:

$route['default_controller'] = 'carta';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['elementos'] = 'carta/elementos';

model:

<?php
class carta_model extends CI_model {

    public function getElementos() {


        return $this->db->get("elemento")->result_array();
    }


} ?>

view:

<?php  echo json_encode($dados);?>

file . htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

1 answer

0

Hi, default controller is the default controller for when you access your URL. If you put it as $route['default_controller'] = 'home'; your controller will be home and will always implicitly access the index method that is the standard of all CI_Controller, this applies to any controller that derives from CI_Controller. If you want it to access a direct method by default use $route['default_controller'] = 'home/index2'; for example. Routes with Codeigniter work like this contoller/metodo/var1/var2/var3...

Browser other questions tagged

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