Routing issue

#1
[eluser]jareep76[/eluser]
I have a route (mainbrand.php) and custom routes that redirect to it like so:

$route['affiliate1/(:any)'] = "mainbrand/$1";
$route['affiliate2/(:any)'] = "mainbrand/$1";

I'm doing this so that I can reskin the user interface based on the affiliate (because the content is identical).

I'm getting a 404 error if I go to any of the affiliate routes defined above.

I'm tried changing the config uri value to all of the options available and none of them work. I'm running a MAMP environment (Apache 2.2 and PHP 5.3.15) with the latest CodeIgniter installed.

This is my .htaccess file (and I've removed index.php from the config as necessary):
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Any assistance is greatly appreciated. Thanks.

#2
[eluser]Aken[/eluser]
Are other pages affected, or is this the only one giving you a 404? Is the 404 generated by CodeIgniter or your server?

#3
[eluser]jareep76[/eluser]
It affects all of the affiliate pages. None of the routes are working. The 404 is a CodeIgniter error. However, I have sense worked around the problem in POSSIBLY a more elegant solution.

I added controllers for each of the affiliates and simply extend the mainbrand controller. This also has the added benefit of allowing me to overload some of the function should the need arise in the future. Thus, no weird routing needed. The affiliate controllers are (for the time being) simple:

class Affiliate1 extends Mainbrand {
function __construct() {
parent::__construct();
}
}

Voila...done. :-) Thanks for the reply.

#4
[eluser]Aken[/eluser]
Well I meant other non-related parts of your application. If they have 404s, too, it would be a more broad problem. If your affiliate links are the only ones getting 404s, then it's more specific. Since you didn't show all of your code, I can't say if there was another problem.

I'd also like to see how you laid out that controller file, because a controller won't know where to find the Mainbrand class unless you require it manually, or it's in your MY_Controller.php core file.

#5
[eluser]jareep76[/eluser]
All of the other functions within the mainbrand controller worked fine. Right now since I'm in the early stages of development, mainbrand was the only controller I had (if you don't count the routes).

As for my new solution, I added a require_once to the top of the affiliate controllers so that I am able to extend and thus overload the mainbrand class.

#6
[eluser]Aken[/eluser]
Yeah that's what I figured. Not the most CI-esque solution, but it works.

By the way, I don't know what URLs you were originally having problems with, but if you went to example.com/affiliate1, without anything else on it, you'd get a 404. Your route will only match when there's something after affiliate1/2 as well, like example.com/affiliate1/viewsomething.

#7
[eluser]jareep76[/eluser]
Do you have a suggestion for a more CI-esque solution or how to correct the route that I need? I tested your theory of adding a method in the url and you are correct that the affiliate loaded fine with the extra segment. I was hoping the the /(:any) would take care of any method names and match a missing segment, as well. Guess I was wrong. How do I fix that? Thanks.

#8
[eluser]jareep76[/eluser]
I think I figured it out. If I understand it correctly, I will need to $route definitions for each affiliate to handle what I need:

$route['affiliate1'] = 'mainbrand';
$route['affiliate1/(:any)'] = 'mainbrand/$1';

Is that A) more CI-esque (because admittedly, I'm fairly new to CI) and B) a better way of handling what I need?

I am almost inclined to leave my sub-class solution in place because of the ability of overloading the functions should the need arise in the future. Thoughts?

#9
[eluser]Aken[/eluser]
Routes use regular expressions to match. If you're unfamiliar with them, do some research; it might help understand routing more. You could get semi-complicated and have one route to cover both affiliate links:

Code:
$route['affiliate(?:1|2)(/.*)?'] = 'mainbrand$1';

The "$1" would cover anything after affiliate1/affilate2, including nothing.

If you think you might want to override methods in the future, I'd look into creating a base MY_Controller instead, and put all of your common methods in there. That's the "CI-esque way" of doing it. You could use requires, though, if you want. It's still valid PHP. Read about using a base controller and see if you like that better. If not, stick with what you're doing.

#10
[eluser]jareep76[/eluser]
I already am using a base controller.

Like so:

Code:
class MY_Controller extends CI_Controller  {
    public $needs_auth = false;
    public $m_config = array();
    public $breadcrumb = array();
    
    function __construct() {
        parent::__construct();
        $this->load->helper(array('url', 'breadcrumb'));
$this->load->library('tank_auth');
        
        // Load site specific config file
        $this->config->load($this->uri->segment(1, 'mainbrand'), true, true);
        $this->m_config = $this->config->item($this->uri->segment(1, 'mainbrand'));;
    }
    
    function render_template($data=array(), $template='template/default') {        
        $data['config'] = $this->m_config;
        
        $this->load->view($template, $data);
    }

    function index() {
        if ($this->uri->segment(1) == false)  redirect('/mainbrand');
        
        if (!$this->tank_auth->is_logged_in() &&
                ($this->needs_auth || $this->config->item('needs_auth', $this->uri->segment(1, 'mainbrand')))) {
            redirect('/auth/login/');
        }
    }
    
    function _addBreadcrumb($title, $url) {
        $this->breadcrumb[]= array('title'=>$title, 'url'=>$url);
    }
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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