How to change url of GET method form to slashes in

2019-08-07 13:43发布

问题:

This question already has an answer here:

  • How to extract get variable from query string in codeigniter? 1 answer

I can not find a solution to my problem, if my question is a duplicate, please provide a reference link. so I need your help. Here's for example:

localhost/ci/index.php/search/?song=sheila+on+7

to be

localhost/ci/index.php/search/sheila_on_7

or

localhost/ci/index.php/search/song/sheila_on_7

Thanks.

回答1:

CodeIgniter optionally supports this capability, which can be enabled in your application/config.php file. If you open your config file you'll see these items:

$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';

If you change "enable_query_strings" to TRUE this feature will become active. Your controllers and functions will then be accessible using the "trigger" words you've set to invoke your controllers and methods:

index.php?c=controller&m=method

More Information can be found in the CodeIgniter URLs users guide



回答2:

You have to change form method from GET to POST. Inflector helper has function you can use, or you can use str_replace() PHP function too. Than something like this:

<?php defined('BASEPATH') OR exit('Not your cup of tea');

class Search extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('inflector');
    }

    public function song($song)
    {
        $song = underscore($this->input->post('song'));//name of field in view which becomes sheila_on_7

        //use variable for DB search or what ever you need

        //$data['song'] = $this->Song_m->get('title', $this->input->post('song'));

        //$this->load->view('search', $data);

    }
}