Welcome Guest, Not a member yet? Register   Sign In
CI is adding URL parts to links
#1

(This post was last modified: 11-13-2014, 02:18 AM by milosh.)

Hello,

I am fairly new to CI and have a problem that is driving me crazy. On my site I have a top navigation for categories, like download, home and so on and each category has a sidebar menu that belongs to the category.

Because I don't want to add a controller for every category, I am trying to do it with a generic controller and view, the model gets the right data for the sidebar menu.

A typical top link would be: domain.ch/home or domain.ch/unix

A typical sidebar link would be:
domain.ch/home/news or
domain.ch/unix/oneliners

Everything after the first / are arguments for the controller because I route the requests to it:

$route['default_controller'] = 'pages/index';
$route['(:any)/(:any)'] = 'pages/index/show/$1/$2';
$route['(:any)'] = 'pages/index/show/$1';

So I am always calling the controller "pages", method "index" with the argument "show" (which loads the show.php view) and one or two optional parameters. The first optional is always the category, the second the section which corresponds to a sidebar link.

Therefor when I click on "home/news" in the sidebar, it should route to "pages/index/show/home/news". The model pulls the right data for the news section belonging to the category home.

Now for the driving-me-crazy-part: This works for the first time I access the site:
click on toplink unix -> sidebar with correct links for unix is shown
click on toplink home -> sidebar with correct links for home is shown
click on sidebar link home/news -> great, the news are pulled and showed on the side.
click again on sidebar link home/news -> 404 page not found. The reason being, that CI is putting another "home" before "home/news", turning the link into "home/home/news". This is only seen in the browser while hoovering over the link. The HTML still shows only "home/news". So for some reason, CI seems to alter the base url and adds the category name to my links every time I click on a link.

I hope someone can hint me in the right direction, I've spend already 10 hours chasing this bug in my code. Is there something wrong with the whole concept how I want to achieve a database driven generic menu or am I missing something? I suspect it's something with routing or .htaccess but I tried virtually everything I could find on google and nothing solved the problem.

Here are the relevant files:
.htaccess:
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [L]

routes.php:
$route['default_controller'] = 'pages/index';
$route['(:any)/(:any)'] = 'pages/index/show/$1/$2';
$route['(:any)'] = 'pages/index/show/$1';

Pages.php (controller)
<?php
class Pages extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->model('home_model');
}

public function index($page = 'show', $category = 'home' , $section = NULL)
{
$data['toplinks'] = $this->home_model->get_toplinks();
$data['sidelinks'] = $this->home_model->get_sidelinks($category);
$data['articles'] = $this->home_model->get_articles($section);
$data['page'] = $category;
$data['title'] = ucfirst($category); // Capitalize the first letter

$this->load->view('templates/header', $data);
$this->load->view('templates/navigation', $data);
$this->load->view('pages/show', $data);
$this->load->view('templates/footer', $data);

}

}

navigation.php: (top/sidebar view)
<div id="topnav">
<?php foreach ($toplinks as $item): ?>
<a href="<?php echo $item['link'] ?>" target="_self"><?php echo $item['text'] ?></a>
<?php endforeach ?>
</div>
<hr>
<div id="leftnav">
<table>
<?php foreach ($sidelinks as $item): ?>
<tr><td>
<a href="<?php echo $page.'/'.$item['link'] ?>" target="_self"><?php echo $item['text'] ?></a>
</td></tr>
<?php endforeach ?>
</table>
</div>

TL;DR: go to http://domain.ch and click several times on "News" in the left sidebar menu, observe the browser address window while hovering with the mouse. Everytime one clicks on news, a new "home" is getting infront of the path, /home/news -> /home/home/news and so on. Why is this happening?

Thank you very much Wink
Reply


Messages In This Thread
CI is adding URL parts to links - by milosh - 11-07-2014, 08:02 AM
RE: CI is adding URL parts to links - by kilishan - 11-07-2014, 12:06 PM
RE: CI is adding URL parts to links - by milosh - 11-07-2014, 12:29 PM
RE: CI is adding URL parts to links - by kilishan - 11-09-2014, 08:29 PM



Theme © iAndrew 2016 - Forum software by © MyBB