Welcome Guest, Not a member yet? Register   Sign In
Problem routing a URL
#1

[eluser]luismartin[/eluser]
Hello, please I need your help with this:

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

www.example.com/news
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:

Code:
// 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
#2

[eluser]PhilTem[/eluser]
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...
#3

[eluser]luismartin[/eluser]
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.
#4

[eluser]PhilTem[/eluser]
Ah okay, so I kinda got your post correctly, but not 100%.

Let's make it like that

Code:
$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

Code:
<?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:
Code:
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

Code:
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

Code:
$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:
Code:
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
#5

[eluser]luismartin[/eluser]
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.
#6

[eluser]PhilTem[/eluser]
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
#7

[eluser]luismartin[/eluser]
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:

Code:
public function index($offset = 0) {

    echo 'hello there!'; exit;

    /* rest of actions */
}

but it's not displayed. I get a 404 error.
#8

[eluser]luismartin[/eluser]
I've just seen where the problem is. Very simple. How didn't I notice? :-S
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. :zip:

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




Theme © iAndrew 2016 - Forum software by © MyBB