Welcome Guest, Not a member yet? Register   Sign In
reroute all methods inside default controller to base
#1

[eluser]phazei[/eluser]
Right now I have my default controller: main

If someone goes to domain.com it automatically goes to main/index

But if someone goes to domain.com/about, and about is in the main controller,
it needs to be rerouted.

Right now I'm rerouting all my main/methods to main:
Code:
$uri_short_list = array('home','profile','account','about','faq','contact');
foreach ($uri_short_list as $uri_short) {
    $route[$uri_short] = 'main/'.$uri_short;
}

Is there a way to use regex to route all domain.com/$1 to domain.com/main/$2

But here's the caveat: only if that method doesn't exist as a controller somewhere else.
Eg: If I have a controller: 'account', then if someone goes to domain.com/account, it won't get directed to main/account, but will go to the account controller.

Thanks.
#2

[eluser]TheFuzzy0ne[/eluser]
Sounds too complicated to me. The controller would have to be loaded, and then another one loaded after if the specified method doesn't exist. I'm not sure if it's a smart thing to do...

You'd be better off sticking to your current method.
#3

[eluser]darkhouse[/eluser]
I extended the Router library (based on Peter Goodman's extension for multiple nested controller folders) to achieve exactly this. I changed the controller to load for you. Just copy and save as application/libraries/MY_Router.php - here's the code.

Code:
<?php

/**
* Router Extension to allow controllers in unlimited nesting of folders.
* Discussion thread at: http://ellislab.com/forums/viewthread/56100/
* @author Peter Goodman
* @copyright Copyright 2007 Peter Goodman, all rights reserved.
*
* CMS capability by Adam Jackett www.darkhousemedia.com, 2009
*/
class MY_Router extends CI_Router {
    
    function _set_route_mapping() {
        parent::_set_route_mapping();
        
        // re-routed url
        if($this->rsegments != $this->segments) {
            array_unshift($this->rsegments, $this->directory);
        }
    }
    
    function _pluck_directory($segments) {
        $this->directory = '';
        
        foreach($segments as $segment) {
            $segment = trim($segment);
            if($segment != '') {
                if(is_dir(APPPATH .'controllers/'. $this->directory . $segment)) {
                    $this->directory .= array_shift($segments) .'/';
                } else {
                    break;
                }
            } else {
                array_shift($segments);
            }
        }
        
        // quick an easy forced reindexing
        $segments = array_values($segments);
        
        // put the entire directory path back into the segment as the first
        // item
        $dir = trim($this->directory, '/');
        if(!empty($dir)) {
            array_unshift($segments, $dir);
        }
        
        $this->segments = $segments;
        
        return $segments;
    }
    
    /*function _validate_request($segments) {
        return parent::_validate_request($this->_pluck_directory($segments));
    }*/
    
    function _validate_request($segments) {
        $segments = $this->_pluck_directory($segments);
        $found = TRUE;
        
        // 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]);
                    $found = FALSE;
                }
            }
            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;
            if($found) return $segments;
        }

        // Can't find the requested controller...
        //show_404($segments[0]);
        $this->set_directory('');
        $this->set_class('main');
        $this->set_method('index');
        return array();
    }

}

Now you can make your main/index function parse the URI string to get whatever content you need. I used this as a basic CMS for a client, but it needed the same ability, only use it if a controller doesn't exist. Then when I parse the URI string, if no content exists, show404()
#4

[eluser]xwero[/eluser]
controllers don't come out of nowhere, why make the routing more difficult than it is?
Code:
$route['(home|profile|about|faq|contact)'] = 'main/$1';
The account segment doesn't need to be rerouted.
#5

[eluser]darkhouse[/eluser]
I my case, the client has the ability to create pages with whatever uri they wish.
#6

[eluser]xwero[/eluser]
darkhouse phazei wants to route to a method inside the main or a controller that exist. If you have all the information why make your application go slower by doing checks that aren't needed?

In case of dynamic urls i would change the content of the routes.php file or include another file with the client routes and route all requests to a controller that acts like a router.
#7

[eluser]darkhouse[/eluser]
I only added a few lines of code to that Router extension, those functions are barely different than what's in the system Router library. All it does is instead of displaying a 404 error if it can't find the controller, it loads a specific controller, in my case a CMS controller.

I don't think creating another file to grab all of the possible URI's from the database to setup a new rule is the way to go, at least not for me. That rule would be ginormous in my file, the client has created plenty of pages, and subpages, and sub-subpages. Adding a few lines to the Router to load a CMS controller seems like a better solution to me.
#8

[eluser]phazei[/eluser]
Coolness. Wink
#9

[eluser]xwero[/eluser]
If the frontend is all client routes and the backend is found in one directory the routes.php file could look like this.
Code:
$route['(admin)(.*)'] = '$1$2';
$route['(.+)'] = 'pages/client/$1';
And that should be all the router needs to know. Then in your controller you can do a search for the user added segments.
Code:
if( ! $this->model->route_exists($this->uri->uri_string()) )
{
   // 404
}
else
{
   // build page
}
#10

[eluser]darkhouse[/eluser]
Hehe, but it's not. I have custom controllers in there as well. I really appreciate you trying to find a better way to do this just through routing, but it really wasn't possible, believe me I tried a number of things before extending the Router.




Theme © iAndrew 2016 - Forum software by © MyBB