Welcome Guest, Not a member yet? Register   Sign In
Routing problems
#1

[eluser]dalebotha[/eluser]
Hi!

I'm having problems understanding how routing works. I read the users guide but I haven't been able to apply it to what I'm doing. Help will be appreciated!

I've got:

- Pages Controller pointed to a pages table in my db.
- I'd like each entry in the pages table to look like this in the URL
home -> www.eg.com/home
about -> www.eg.com/about

Here is my index action in Pages Controller:
Code:
function Pages()
        {
            parent::Controller();
            
            $this->table = 'pages';
            $this->view_type_table = 'view_types';
                        
            $this->load->model('Pages_model', 'page', true);
            
            $this->template['navigation'] = $this->page->nav();
        }
function index()
        {

            
            if ( ! $path = $this->uri->segment(1))
            {
                $path = 'home';
            }

            if ( $this->template['page'] = $this->page->get($path) )
            {
                if ( $type_id = $this->template['page']['type_id'] )
                {
                    $view_type = $this->page->get_view_type($type_id);
                    $this->template['view_type'] = $view_type['name'];
                    $this->_run();
                }
                
            }
            else
            {
                die('404! oops... :(');
            }
            
        }

I watched Elliot Haughin's screencasts so a lot of this come from him. This is in my httpd.conf:
Code:
<Directory "/absolute/url/codeignitor/">

Options Indexes Includes FollowSymLinks MultiViews
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
AllowOverride All

</Directory>
...and this is my .htaccess:
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

Is this enough info to have a clue? It seems there are soo many factors a play here that I really have no clue where it's going wrong and how to debug it!

Would anybody be able to help me?

Thanks, Dale
#2

[eluser]roj[/eluser]
Have you added anything to the routes file?
#3

[eluser]dalebotha[/eluser]
This is in routes file:
Code:
$route['default_controller'] = "pages";
$route['scaffolding_trigger'] = "";

$pages = array('home');

foreach ($pages as $page) {
    
    $route[$page] = $page;
    
    $route[$page.'/(.*)'] = $page."/$1";
}

$route['(.*)'] = "pages/index/$1";

These routes could be total garbage as I really am clueless and don't know how to get clued up.
Wink
#4

[eluser]Colin Williams[/eluser]
Well, clearly in your loop, you are routing 'home' to 'home' and 'home/something' to 'home/something'. Quite useless. I think you want to route 'home' to 'pages/index/home'

Code:
$route['default_controller'] = "pages";
$route['scaffolding_trigger'] = "";

$pages = array('home');

foreach ($pages as $page)
{
    $route[$page] = 'pages/index/'. $page;
    $route[$page .'/(.*)'] = 'pages/index/'. $page ."/$1";
}
#5

[eluser]roj[/eluser]
I think the first one was right. The setup is reasonable simple:
Code:
$route['(.*)'] = "pages/index/$1";
Will pass all requests to the page controller and specifically the index function.

By doing this you set up a simple way to create individual pages. The trouble is that say you want to add a news section to the site. In order to do that you probably want to send those requests to it's own controller. ie set up an exception to the above route.

The proceeding bit is to handle any exceptions so with the $pages array you add sections that you don't want to send to the pages controller. So the array would read:
Code:
$pages = array('news');

The foreach loop then takes each item in the array and writes a route sending it where you would normally find it (as Colin pointed out) eg the news controller.

Say you also wanted to add an admin section and authors section then you just add admin & author to the $pages array:
Code:
$pages = array('news','admin','author');

...and obvious create the corresponding controllers as normal.

Hope that adds a little clarity.




Theme © iAndrew 2016 - Forum software by © MyBB