• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help for htacces or routing

#1
I have this scenario:

One controller called Brands with relative class Brands.
Inside this controller the following functions:

index() - This is showing the list of all brands inside my db
show($slug) - This is showing the detail of the brand clicked and all list of the products related with that brand.
detail($slug) - This is showing the detail of one product belonging to the previous product list

When I call index, as normal, I have a url like that:

- http://localhost/brands

When I click on a brand of this list, I have an url like that:

- http://localhost/brands/show/apple

When I click on a product of this list, I have an url like that:

- http://localhost/brands/detail/macbook-13-retina

I would like to have previous urls in this form:

- http://localhost/brands
- http://localhost/brands/apple
- http://localhost/brands/apple/macbook-13-retina

Now, I've read a lot of tutorials and I've read how to routing library works, but
I can't understand if routes can help me more than a htaccess.
Also, the cms I'm building can be installed with a different language than English, so I can have
for example:

- http://localhost/brands but as well
- http://localhost/marcas or
- http://localhost/marche

How can I use routing or htaccess for to get the above changes?

Can someone help me? Thank you very much in advance...

Giorgio
Reply

#2
Hi,

I think the language part of your question is a seperate issue all by itself.

In response to this:

Code:
- http://localhost/brands
- http://localhost/brands/apple
- http://localhost/brands/apple/macbook-13-retina

Without routes you could easily achieve this in one controller.

controller Brands.php
PHP Code:
public function index($brand=''$product='') {
 
    ...
 
    if (!empty($product)) {
 
         // call your product model and do whatever
 
         // or redirect without a refresh
 
    } else if (!empty($brand)) {
 
         // call your brand model and do whatever
 
    } else {
 
        // show all brands
 
    }


Or with routes you could do
PHP Code:
$route['brands/:any/:any'] = 'catalog/product_lookup/$1/$2';
$route['brands/:any'] = 'catalog/brand_lookup/$1';
$route['brands'] = 'catalog/all_brands'

Hope that helps,

Paul.

PS Routes docs are here: https://www.codeigniter.com/user_guide/g...uting.html
Reply

#3
Thank you for your fast reply! I will try as soon as possible!
Giorgio
Reply

#4
Then of course, once that is working, you could add:

PHP Code:
$route['marcas/:any/:any'] = 'catalog/product_lookup/$1/$2';
$route['marcas/:any'] = 'catalog/brand_lookup/$1';
$route['marcas'] = 'catalog/all_brands'

And similar for other languages. But that really depends how you choose to go about implementing a multi-lingual site. There are lots of options and no real 'best' answer, just what suits your site.

Paul.
Reply

#5
Hi Paul and thank you one more time for your help
I was reading with attention the route config file guide and I have to say that it was a misunderstanding in my topic.

So route is:

Code:
$route['brands'] = 'categories';

This example above says that if routing class found in the url the word 'brands', that link is remapped to a controller categories.
So 'categories' is something that already have to exist. This is a remapped.

In fact, putting:
Code:
$route['brands'] = 'categories';
is correct, but putting
Code:
$route['brands'] = 'my_word_is_not_a_controller_or_function';
I receive a warning...

I don't want a remapped action, I want a rewrite action.
In my idea, I'd like that the normal class/method/param were mapped correctly but with other words, beacuse of the slugs are depending by the user that introduces data...

I hope I explain good myself...

Please tell me if my thought is correct...

Giorgio Shy
Reply

#6
After days and days I solved my problem...
I interpreted in a bad way your reply, but after I understood it and I solve it.
Thanks a lot!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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