user9917793
user9917793

Reputation: 13

CodeIgniter - URL routing for more cases

Here is what I want to achieve:

https://www.example.com/properties
https://www.example.com/properties/properties-in-newyork
https://www.example.com/properties/properties-in-DC/property-for-rent
https://www.example.com/properties/all-cities/property-for-rent
https://www.example.com/properties/all-cities/property-for-sale

All above is for search. Now I want to get details page like:

https://www.example.com/properties/2br-apartment-for-sale-100

I want to differentiate between search and details page links. Here is what I tried:

$route['properties/index']  = 'properties';
$route['properties(/:any)'] = 'properties/property_details$1';

How can I differential which URL is for properties/property_details function and which URL is for properties/index function? enter image description here

Upvotes: 1

Views: 105

Answers (2)

NSJ
NSJ

Reputation: 13

If I'm correct, according to your explanation with differentiating the routes, the problem you are having is, it always running the route for index despite of what your URL having after properties.

You may try it by changing the order of the routes like this;

$route['properties(/:any)'] = 'properties/property_details/$1';
$route['properties/index']  = 'properties';

It always works according to the order of the routes you have placed. If there are acceptable parameters, for the program, properties/index is also something similar to properties(/:any). So, to differentiate between these two, we have to change the order of the routes like this.

Upvotes: 0

Pradeep
Pradeep

Reputation: 9707

Set your route.php like this :

$route['properties/index']  = 'properties';
$route['properties'] = 'properties/property_details';
$route['properties/(:any)'] = 'properties/property_details/$1';

Access url :

this direct you index method

https://www.example.com/properties/index

this will direct you property_details method

https://www.example.com/properties/

Controller :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Properties extends CI_Controller {
    public function __construtct()
    {
        parent::__construtct();
        $this->load->helper('url');
    }

    public function index()
    {
        echo 'index';
    }

    public function property_details($component = NULL)
    {
      echo 'property_details';
      echo $component;
    }

}

Upvotes: 1

Related Questions