• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
conditional routing (shorten url)

#1
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
Reply

#2
@acebay,

Nothing in the current CI documentation about routing can help you ( https://www.codeigniter.com/userguide3/g...ri-routing )?
Reply

#3
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.
Reply

#4
Can I shorten my url with .htaccess? Is it possible in my case.

Say I have these urls in my website

http://www.example.com/locations

http://www.example.com/locations/sydney

http://www.example.com/locations/brisbane

http://www.example.com/locations/perth

http://www.example.com/services/

http://www.example.com/services/cars

http://www.example.com/services/truck

http://www.example.com/services/phone

http://www.example.com/blog

http://www.example.com/blog/display/blog-title

http://www.example.com/blog/display/blog-title2

http://www.example.com/blog/display/blog-title3

http://www.example.com/blog/display/blog-title4

what I want is the urls to be like these

http://www.example.com/locations

http://www.example.com/sydney

http://www.example.com/brisbane

http://www.example.com/perth

http://www.example.com/services/

http://www.example.com/cars

http://www.example.com/truck

http://www.example.com/phone

http://www.example.com/blog

http://www.example.com/blog-title

http://www.example.com/blog-title2

http://www.example.com/blog-title3

http://www.example.com/blog-title4

how to achieve this in codeigniter??
Reply

#5
(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
Reply

#6
@acebay,

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

#7
(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
Reply

#8
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.
Support Development  • Practical CodeIgniter 3  •
Myth:AuthVulcan - CLI Tools for CI4
Reply

#9
(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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.