user254153
user254153

Reputation: 1883

Codeigniter redirect from controller as set in routes config

I want to redirect the page to the url that is set in routes config.

class init extends CI_Controller {

function __construct() {
    parent::__construct();

    $this->load->helper("url");
    $this->load->model('store');

}

function product($id, $slug=''){

    //if no slug value passed then we obtain the product title and redirect
    if($slug === ''){

        //get the title value from products table and run it through _slugify function
        $slug = $this->_slugify($this->store->read_productTitle($id));

        redirect('init/product/'. $id . '/' . $slug);  //okey when redirect
       // redirect('product/'. $id . '/' . $slug);     redirect loop

    }


}

In routes.php

 $route['product/(:num)/(:any)'] = 'init/product/$1/$2';

When I redirect to redirect('product/'. $id . '/' . $slug) then It displayes webpage has redirect loop. How can I solve this without making product controller.

Upvotes: 1

Views: 9376

Answers (1)

Praveen Kumar
Praveen Kumar

Reputation: 2408

Change your class code to mine

class Init extends CI_Controller //mark first letter uppercase
{

function __construct() {
    parent::__construct();

    $this->load->helper("url");

}

function product($id, $slug='')
{

    //if no slug value passed then we obtain the product title and redirect
    if($slug === '')
    {        
        $slug = 'hello'; //$this->_slugify($this->store->read_productTitle($id));
        redirect('product/'. $id . '/' . $slug);   
    }
    else
    {
        echo $slug;
    }       
}

}

And in routes change

$route['product/(:num)/(:any)'] = 'init/product/$1/$2';

Your redirect is ot working because you were redirect on same fuunction from same function :)

Upvotes: 2

Related Questions