Welcome Guest, Not a member yet? Register   Sign In
Best way to this url rewriting and loading other controllers?
#1

[eluser]inet411[/eluser]
I'm experimenting with codeigniter.

I like to have search engine friendly urls like this:
example.com/honda/accord or
example.com/honda

where honda is the category and accord is the page.
I can do this easily with procedural programming like so:

.htaccess
Code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

index.php
Code:
// I put some code here to explode the url and get it into segments.....
// so $category = honda and $page = accord
if(isset($page)) {
    include('page.php');
} elseif(isset($category)) {
    include('category.php');
}

based on the two urls above the first one would include page.php the second would include category.php
then the category.php page does whatever a category page does and the page.php page does whatever a page page does.

But with codeigniter I have the same .htaccess file and here is my index.php (welcome.php)

welcome.php
Code:
class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    
    function index()
    {
        if(segment(1) && segment(2)) {
            //how do I include the category.php controller?
        } elseif(segment(1)) {
            //how do I include the page.php controller?
        }
    }
}


I understand I can use urls like:
example.com/category/honda/page/accord
example.com/category/honda
And that will do exactly what I want ...
But I don't want the words category or page in the url. I want the urls that I showed above.

Any suggestions?
What about creating a new method so I can $this->load->controller('category.php'); // is that possible? or would that have side effects?
#2

[eluser]n0xie[/eluser]
Take a look at URI Routing
#3

[eluser]inet411[/eluser]
[quote author="n0xie" date="1262901205"]Take a look at URI Routing[/quote]

Believe me I have read it... and read it again...
Like I said I see that it is possible by have the urls as:
example.com/category/honda/
then it would load the category controller which is great but I want this url
example.com/honda
I never know what the /honda is going to be because it is dynamic. So I can't create a honda controller.
Please read the first post again to see what I'm talking about.
I saw wick but it seems a little unstable.
#4

[eluser]inet411[/eluser]
If anyone has any ideas/suggestions please help.

So I was also thinking ... I know that if there is one segment I want to load the category.php controller but if there are two segments I know I want to load the page.php controller.

If what I've asked in my first post is not possible... Do you think I could make something like a MY_Router class and check the number of segments in the url and then route to one or the other controller based on that? If so (I apologize for asking but I'm at my wits end here) could you post a small snippet to get me started?
#5

[eluser]n0xie[/eluser]
This might work. I haven't tested it. Create a category class and a page class both with a 'show' function.

Code:
class Category extends Controller {

    function Category()
    {
       parent::Controller();

    }

    function index() {}
    function show($category) {}
}

Code:
class Page extends Controller {

    function Page()
    {
       parent::Controller();

    }

    function index() {}
    function show($category, $page) {}
}

Then extend the Router class (put in application/libraries).

Code:
class MY_Router extends CI_Router {

    function MY_Router()
    {
        parent::CI_Router();
    }

    // copied from original CI_Router class
    function _validate_request($segments)
    {
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            if (count($segments) > 0)
            {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }

            }


            return $segments;
        }

        
        // part that matters
        if (count($segments) == 1)
        {
            $segments = array('category','show',$segments[0]);
            return($segments);
        }
        elseif (count($segments) == 2)
        {
            $segments = array('page','show',$segments[0], $segments[1]);
            return($segments);
        }
        
        show_404($segments[0]);
    }




Theme © iAndrew 2016 - Forum software by © MyBB