Welcome Guest, Not a member yet? Register   Sign In
Best way to this url rewriting and loading other controllers?
#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]);
    }


Messages In This Thread
Best way to this url rewriting and loading other controllers? - by El Forum - 01-07-2010, 11:13 AM



Theme © iAndrew 2016 - Forum software by © MyBB