Best way to this url rewriting and loading other controllers? |
[eluser]inet411[/eluser]
I'm experimenting with codeigniter. I like to have search engine friendly urls like this: example.com/honda/accord or example.com/honda where honda is the category and accord is the page. I can do this easily with procedural programming like so: .htaccess Code: RewriteEngine on index.php Code: // I put some code here to explode the url and get it into segments..... based on the two urls above the first one would include page.php the second would include category.php then the category.php page does whatever a category page does and the page.php page does whatever a page page does. But with codeigniter I have the same .htaccess file and here is my index.php (welcome.php) welcome.php Code: class Welcome extends Controller { I understand I can use urls like: example.com/category/honda/page/accord example.com/category/honda And that will do exactly what I want ... But I don't want the words category or page in the url. I want the urls that I showed above. Any suggestions? What about creating a new method so I can $this->load->controller('category.php'); // is that possible? or would that have side effects?
[eluser]inet411[/eluser]
[quote author="n0xie" date="1262901205"]Take a look at URI Routing[/quote] Believe me I have read it... and read it again... Like I said I see that it is possible by have the urls as: example.com/category/honda/ then it would load the category controller which is great but I want this url example.com/honda I never know what the /honda is going to be because it is dynamic. So I can't create a honda controller. Please read the first post again to see what I'm talking about. I saw wick but it seems a little unstable.
[eluser]inet411[/eluser]
If anyone has any ideas/suggestions please help. So I was also thinking ... I know that if there is one segment I want to load the category.php controller but if there are two segments I know I want to load the page.php controller. If what I've asked in my first post is not possible... Do you think I could make something like a MY_Router class and check the number of segments in the url and then route to one or the other controller based on that? If so (I apologize for asking but I'm at my wits end here) could you post a small snippet to get me started?
[eluser]n0xie[/eluser]
This might work. I haven't tested it. Create a category class and a page class both with a 'show' function. Code: class Category extends Controller { Code: class Page extends Controller { Then extend the Router class (put in application/libraries). Code: class MY_Router extends CI_Router { |
Welcome Guest, Not a member yet? Register Sign In |