0

I've explored lot of questions and articles regarding this but I can't find how to get this done.

I'm doing a website which provides specifications of several products such as phones, tablets, tv etc. Here's what I've:
Controller - Specs (create and display specification of all products)
Method - Display (fetches detailed specs of selected model and shows)
Method - Index (lists names of all models stored in the table. this is where I build anchor links)

Display method takes three arguments (1, 2, 3).
1 - Type of product (Phones, Tablets, TV etc)
2 - Model Slug (iphone-6, galaxy-tab-s3, bravia-kdl-50w800d etc)
3 - Model ID (1, 4, 13 etc)

My URLs right now are like this:
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13

What I want to achieve is URLs which are like this:
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d

I don't mind restructuring my tables/controllers/methods or anything else if this can be achieved using whatever.

Thanks for reading.

Edit:
Route.php

$route['default_controller'] = 'Specs/index';
$route['404_override'] = 'Errors/show_404';
$route['translate_uri_dashes'] = FALSE;

This is how I'm building the anchor links (view_file->index.php, called from Index method):

<?php 
    foreach model(in the table)
        echo anchor(specs_controller.display_function.product_type.model_slug.model_id, model_name);
    end foreach
?>
Kabeirs
  • 3
  • 3
  • can you please share your route.php code and also of view file where you click on product button to get product data – Deepak Jul 10 '18 at 10:02
  • edit your question and add there your route.php lines. – Deepak Jul 10 '18 at 10:51
  • If you're looking for how I'm building the anchor link for products, it goes like this : "echo anchor(controller/function/product_type/model_slug/model_id, model_name)" – Kabeirs Jul 10 '18 at 10:58
  • refer this https://stackoverflow.com/questions/14783666/codeigniter-htaccess-and-url-rewrite-issues – Deepak Jul 10 '18 at 12:04
  • you need to modify your .htaccess file. – Deepak Jul 10 '18 at 12:05
  • I have tried several modifications to route.php, nothing worked. Can you guide on what exactly I need to update in route.php? – Kabeirs Jul 10 '18 at 12:26

3 Answers3

0

I can get the desired URLs with following code in route.php. Only problem is I'm not able to make the 'urlController/urlMethod' return a value in the function which can be assigned to $result variable.

$route['(:any)'] = function ($1)
{
        $result = 'urlController/urlMethod/'.$1;
        return $result;
};

I'm not sure how to do this. Can someone suggest how I should call 'urlController/urlMethod'?

Kabeirs
  • 3
  • 3
  • In a route, the array key contains the URI to be matched, while the array value contains the destination it should be re-routed to. So what ever you return from this function is supposed to be a URL – Tarun Kumar Jul 10 '18 at 13:39
  • I've actually created a new table **url_mapping** which contains shortened urls (such as **1142-iphone-6** and a corresponding full url with controller, method and parameters such as **(specs/display/phones/iphone-6/1)**. This shortened url contains all required IDs in the first part. What I want is pass this short url to the function as parameter which in turn will call a fix 'urlController/urlMethod' everytime which will resolve and find its corresponding full url and return that to the function. Function will then return it and map to $route(:any). – Kabeirs Jul 10 '18 at 13:52
0

You could achieve it with CodeIgniter URI Routing. Considering

localhost/sitename/galaxy-tab-s3

maps to

localhost/sitename/specs/display/tablets/galaxy-tab-s3/4

And, model id i.e 4, in this case, is static with respect to galaxy tab s3, as you have not mentioned any such Id in the simplified URL.

My understanding is with every URL localhost/sitename/iphone-6, you need three details about the string 'iphone-6'. i.e. type of product, model-slug, model id. One way could be write something like

$route['sitename/(:any)'] = 'routingController/commonRoutingMethod/$1';

Here, create a new routingController and write some logic into commonRoutingMethod() method, which takes the string like iphone-6 and fetches its all three details i.e. product type, model id etc. And then redirects by building the exact URL using

header('Location:  http://localhost/sitename/specs/display/$productType/$modelSlug/$modelId/');

NOTE : There could be more forward ways just using regex match in routes.php, given that you create diffrentiated structure of the string, based on product type and model id e.g p_iphone-6_1 or t_galaxy-tab-s3_4.

Tarun Kumar
  • 201
  • 1
  • 4
  • Thanks. This is exactly what my code is doing right now. The only difference being you are clubbing three parameters to commonRoutingMethod() into one and I've them separated. **Specs** is a common method that processes all product types whether it's Phones, Tablets, TVs or anything else. Going by your suggestions, the resulting URL will still have the name of routingController (**Specs** in this case) and commonRoutingMethod(**Display** in this case). I want all of them removed. Thanks anyway. – Kabeirs Jul 10 '18 at 15:47
  • This answer is supposed to automatically redirect simple URL to complex URL. Do you want this behavior i.e. support simple URL but actually it will be redirected to long URL automatically? Or you want the final URL to be the simple one i.e. no further automatic redirection to any long URL? – Tarun Kumar Jul 10 '18 at 20:27
  • I did implement this but the line with "header(...)" actually redirects to long url taking me where I started from. – Kabeirs Jul 14 '18 at 06:30
  • Now got it, you actually don't want the ultimate URL to be complex. Whereas this code is supposed to provide support for your small URLs while automatically redirecting them to the long URLs – Tarun Kumar Jul 15 '18 at 11:15
0

Please use below routing code, to achieve it.

localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13

localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d

$route['(:any)'] = 'specs/display/$1/$1/$1';

Let me know if you need any help.

Sorav Garg
  • 1,116
  • 1
  • 9
  • 26