CodeIgniter Forums

Full Version: conditional routing (shorten url)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, is it possible to have conditional in routes.php like below

Code:
$req = $this->uri->segment(1);
require_once ( BASEPATH. 'database/DB.php');
$db =& DB();
if ($db->get_where('home_inner_tbl', array('page_url' => $req))) {
   $route['(:any)'] = 'home/display/$1';
} elseif ($db->get_where('blog_tbl', array('page_url' => $req))) {
   $route['(:any)'] = 'blog/blog_display/$1';
} elseif ($db->get_where('towing_services_tbl', array('page_url' => $req))) {
$route['(:any)'] = 'towing-services/display/$1';
} elseif ($db->get_where('special_services_tbl', array('page_url' => $req))) {
$route['(:any)'] = 'cash-for-cars/display/$1';
} elseif ($db->get_where('locations_tbl', array('page_url' => $req))) {
$route['(:any)'] = 'locations/display/$1';
}

I want to shorten the url from www.example.com/locations/sydney to www.example.com/sydney but the code above somehow don't work, it only applies to the first if statement. Any suggestion??

My Home controller is looking like below, I have Locations, Towing_services, Cash_for_cars, and Blog controllers look exactly the like below

Code:
class Home extends CI_Controller
{

public function index() {
// some code in here
}
public function display($page_url) {
// some code in here
}
}

Can anyone help me please. Thanks
@acebay,

Nothing in the current CI documentation about routing can help you ( https://www.codeigniter.com/userguide3/g...ri-routing )?
Because it's config file, it doesn't have access to database.

While it might feel a bit "hacky", you could set up 404 controller, and if route fails, you could still catch it and deal with it, if it's a short hand for existing page.
(01-27-2019, 12:49 PM)Pertti Wrote: [ -> ]Because it's config file, it doesn't have access to database.

While it might feel a bit "hacky", you could set up 404 controller, and if route fails, you could still catch it and deal with it, if it's a short hand for existing page.

Can you please explain more how to hack it with 404 controler? Sorry I'm just new to programming, need some help. Thanks
(01-28-2019, 09:49 AM)php_rocs Wrote: [ -> ]@acebay,

This might be helpful to you ( https://codeigniter.com/user_guide/gener...x-php-file ).

Hi, thanks for the tip, I've read the documents, but there's nothing there that can help shortening the url for my case. Thanks
Here's one potential way to handle it, though it comes with a pretty big caveat:

- In your routes config file do a "catch-all" route as the very last route. That way any route not found or specified above that is sent to the controller/method of your choice.
- In that controller you'd have to parse the URL segments manually to determine what page to show based on your app's business logic.

The big caveat is that this means you MUST manually specify all other routes in the system, instead of relying on CI's automatic routing based on Controller/Method name in the URL.

This same type of setup could be used for the 404 Controller as Pertti said. Check the Reserved Routes section on the URI Routing page to see how to override the 404 action.
(01-28-2019, 02:58 PM)kilishan Wrote: [ -> ]Here's one potential way to handle it, though it comes with a pretty big caveat:

- In your routes config file do a "catch-all" route as the very last route. That way any route not found or specified above that is sent to the controller/method of your choice.
- In that controller you'd have to parse the URL segments manually to determine what page to show based on your app's business logic.

The big caveat is that this means you MUST manually specify all other routes in the system, instead of relying on CI's automatic routing based on Controller/Method name in the URL.

This same type of setup could be used for the 404 Controller as Pertti said. Check the Reserved Routes section on the URI Routing page to see how to override the 404 action.

Hi, thank you for the tips, yeah I did that, I specify all the pages in my routes.php so it will have the URL that I intend (code below).

//////////////////////////////////
$route['about-us'] = 'home/display/about-us';
$route['blog'] = 'home/display/blog';
$route['testimonials'] = 'home/display/testimonials';

$route['car-towing'] = 'towing-services/display/car-towing';
$route['truck-towing'] = 'towing-services/display/truck-towing';
$route['motorbike-towing'] = 'towing-services/display/motorbike-towing';

///// There's more, but you got the point //////////

But the only problem is that I created a backend that can create a new page, so every time I create a new page I have to write the page-url in routes.php, it's not automatic Sad.

If anyone wondering why I'm using flat structure URL instead Silo structure URL, it's for SEO purposes.