简体   繁体   中英

CodeIgniter Custom URI Routing

I want to create custom permalinks on CodeIgniter, actually i bought the script but the developer left that project due to some indifference. so now the problem is i have no idea how to change permalinks on that script. The main permalinks issue is when i search anything on searchbar i get this url:

domain.com/?s=xxxxx%20yyyyy

instead of that i want this url structure:

domain.com/search/xxxxxx-yyyyy/

application/config/routes.php

$route['default_controller']    = "music";

$route['404_override']          = '';

$route['search/(:any)']         = "music/index/$0/$1/$2";

$route['search/music/(:any)']   = "music/$1";

I guess what you are asking for is not possible (directly) .
Assuming your form to be,

<form action="" method="GET">
    <input type="text" name="s" value="" placeholder="Search music..." />
</form>

And since the method is GET the default functionality says to add the parameter in the URL as query string.

As the specifications ( RFC1866 , page 46; HTML 4.x section 17.13.3) state:

If the method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a `?' to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type.


So, basically what you can do here is apply a hack to this. Redirect the user to the required URL when the search is applied.
Here's how you can go,

Controller (controllers/music.php)

<?php
class Music extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('xyz_model');
    }

    public function index()
    {
        if($this->input->get('s'))
        {
             $s = $this->input->get('s');
             redirect('/search/'$s);
        }

        $this->load->view('home.php');
    }


    public function search()
    {
        $s = $this->uri->segment(2);

        /*
        Now you got your search parameter.
        Search in your models and display the results.
       */

       $data['search_results'] = $this->xyz_model->get_search($s);
       $this->load->view('search_results.php', $data);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM