EllisLab text mark
Advanced Search
     
Problem routing a URL
Posted: 09 November 2012 07:15 AM
Joined: 2011-01-22
81 posts

Hello, please I need your help with this:

I previously had two different URL’s leading to different controllers:

http://www.example.com/news
http://www.example.com/reviews

Both were managed differently. The first one manages several items from DB called articles, and the second one was made to display single data stored in DB.

Now I’ve been requested to modify the second URL so that it’s displayed like in “news” for a new type of articles called “reviews”. I would only need to pass some additional variable to the “News” controller in order to differentiate whether I want to view articles of type “news” or articles of the new type “reviews”, and pull the corresponding records from an “articles” table in the DB.

A simple solution I tried was to use the CI routes. So I added a second line to the first one below:

// ignore the initial regular expression. It is just to manage several languages.
// Which is being already done by Apache's mod_rewrite
$route['((\w{2}/)?)news/(.*)''news/index/$3';
$route['((\w{2}/)?)reviews/(.*)''news/index/$3/2'

Number 2 at the end of the 2nd line would tell the “index” method in the “news” controller that I’m trying to view articles of type “review”. I also tried with a request variable: ?type=2

My first attempt to load it, and the old “reviews” controller was still being loaded. Well, this looked strange, since I thought the mapping of the CI routes were applied prior to the controller load. So next I renamed the “reviews” controller. And I got a 404 error. So, the CI routing is not working.

Why? Any advice? Maybe this is not the best way to do it, but is it possible?

PS:
To provide more information about this issue, I must tell you that I previously tried another way:
I created a parent class called Articles which extends from CI_Controller which had all the implementation, with protected methods, and two subclasses called News and Reviews which would inherit the protected methods, like “index”, and would pass a specific variable to their parent. However, this way I got a blank page. No errors displayed or logged. Even no inbetween echoes I inserted

 
Posted: 09 November 2012 07:26 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

I’m wondering, how the URIs
example.com/news
and
example.com/reviews

shall be mixed together to be called via

example.com/news

and either display news or reviews.

Where’s the switch to tell your server to load news OR reviews?
You could easily create a controller called news with two methods
index which displays the news, and
reviews which displays the reviews.
This way you will get URIs lile
example.com/news
and
example.com/news/reviews

If this wasn’t helpful and I may have gotten your message/problem incorrectly, I will need to read your post another time more carefully…

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 09 November 2012 08:35 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-01-22
81 posts

Hello Phil,

I think you didn’t understand what I was trying to explain. I still want to use both URL’s. However the one for reviews will have to be managed in a different way than before. And I was trying to route this URL so that the “News” controller could manage the request.

 
Posted: 09 November 2012 09:24 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

Ah okay, so I kinda got your post correctly, but not 100%.

Let’s make it like that

$route['news/(:any)''news/news/$1';
$route['news''news/news';
$route['reviews/(:any)''news/reviews/$1';
$route['reviews''news/reviews';

// ./application/config/routes.php 

Then, inside ./application/controllers/, create a file called news.php with some content like this

<?php

class News extends CI_Controller { // or MY_Controller, if you're using that one
  
  
function __construct()
  
{
    parent
::__construct();
  
}
  
  
function news()
  
{
    
// Do whatever you need to do to display news
  
}
  
  
function reviews()
  
{
    
// Do whatever you need to do to display the reviews
  
}

Folder structure:

controllers/
  
news.php
    news
::news
    news
::reviews 

You could also create a controller news.php and a controller reviews.php inside a subfolder or ./application/controllers/news/ with content

class {News,Reviews} extends {MY,CI}_Controller {
  
  
function __construct()
  
{
    parent
::__construct();
  
}
  
  
function index()
  
{
    
// If this is controller news.php display the news
    // If this is controller reviews.php, display the reviews
  
}

Then your routes would be

$route['news/(:any)''news/news/$1';
$route['news''news/news/index';
$route['reviews/(:any)''news/reviews/$1';
$route['reviews''news/reviews/index';

// ./application/config/routes.php 

Folder structure:

controllers/
  
news/
    
news.php
      news
::index
    reviews
.php
      reviews
::index 

I hope I didn’t make it more confusing than it actually is, but there a multiple ways to solving your question wink

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 09 November 2012 12:34 PM   [ # 4 ]   [ Rating: 0 ]
Joined: 2011-01-22
81 posts

That’s a good solution, but I’m wondering why mine is not working: routing both URL’s to the same controller including a request variable or additional segment to differentiate them.

 
Posted: 09 November 2012 01:48 PM   [ # 5 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

Yours should actually work, too. What’s the method declaration of your index()-method? Post your index()-method so we can have a look at it. Maybe your bug is hiding somewhere in there wink

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 12 November 2012 12:47 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2011-01-22
81 posts

Hello again Phil,

I’m sure it doesn’t matter at all how’s the declaration of the index() method in this case, since it is not even executed for “reviews” (it is for “news”, it’s original route). I placed an echo to make sure:

public function index($offset 0{

    
echo 'hello there!'; exit;

    
/* rest of actions */

but it’s not displayed. I get a 404 error.

 
Posted: 12 November 2012 01:10 PM   [ # 7 ]   [ Rating: 0 ]
Joined: 2011-01-22
81 posts

I’ve just seen where the problem is. Very simple. How didn’t I notice? confused
It was just a slash. I think I was being too thick on Friday.

$route[’((\w{2}/)?)reviews/(.*)’] = ‘news/index/$3/2’;

I just needed to make it optional. You told me but I didn’t see it. zipper

Now I now how to proceed. Thanks for your help!