Reputation: 103
I am creating a directory site in codeigniter. On landing page load it prompts to select state then city. Then it redirects from www.example.com to www.example.com/location/city-name/.
Here Site is the controller name and location is the function name within it.
I have already removed controller name from the url using route.php. Code is below:
$route['location/(:any)'] = 'site/location';
But I want to remove function name location too from the url and url should be www.example.com/city-name/.
Further when category is selected after selecting the city it should be redirected to www.example.com/city-name/category-name/ but I am able to redirect to www.example.com/location/city-name/category-name/ only using following lines in route.php:
$route['location/(:any)/(:any)'] = 'site/location';
I can capture city and category name using $this->uri->segment and use the same for filter process there is absolutely no issue with it.
But main issue remains to remove the function name location.
I have searched suggested questions before posting this question but could't find any suitable solution. Can anyone help me to achieve the goal?
My site controller is below:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Site extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('file');
$this->load->model('query_model');
$this->load->library("pagination");
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
}
public function index(){
$this->load->view('website/index');
}
public function location(){
$this->load->view('website/index');
}
}
I think I am doing some mistake either in index or location function. Location Function is nothing just showing the view of index. Index view has the code to grab uri segment and filter the data accordingly.
My code which is passing state id to a jquery function:
<a onclick="get_cities(2)">Andaman & Nicobar Islands</a>
My jquery code get_cities(id) is below:
function get_cities(id){
var aj_url="<?= base_url();?>ajax_post_controller/show_cities";
jQuery.ajax({
type: "POST",
url: aj_url,
data: {id: id,},
success: function(res, status) {
if (res)
{
alert(res);
$("#loc-head").html("Select City");
$("#loc-data").html(res);
$(".modal-footer").css("display","block");
}
}
});
}
After defining below code: $route['(:any)'] = 'site'; $route['(:any)/(:any)'] = 'site';
Alert from get_cities function returning html code of index page and Index page is displayed in opened model too.
If I comment above two lines I get the list of cities it returns the url of city
<a href="http://example.com/visakhapatnam/">Visakhapatnam</a>
But due to above 2 route lines commented above city url give an error of 404.
Upvotes: 0
Views: 622
Reputation: 1598
add like this
In routes.php
$route['location/(.*)'] = 'site/index';
In Controller
public function index(){
if($this->uri->segment(1) && $this->uri->segment(2)){
$this->load->view('website/category');
}elseif($this->uri->segment(1)){
$this->load->view('website/city');
}else{
$this->load->view('website/index');
}
}
Upvotes: 0
Reputation: 9707
hope this will help you :
$route['location'] = 'site/index'; /* redirect to index method */
$route['location/(:any)'] = 'site/location'; /* redirect to location*/
$route['location/(:any)/(:any)'] = 'site/location/$1';
Upvotes: 1