Welcome Guest, Not a member yet? Register   Sign In
Help with routes!
#1

[eluser]Mark LaDoux[/eluser]
// updated (got a partial script done now, there's a bug, but it should work.
// If you see where i slipped up, let me know

Ok, what i want to do is this. I want to reroute everything in my default controller in such away that I never have to type the controllers class into the url bar. Basically skip segment one and go right on to segment two. I want to do this dynamically so if I add functions, i don't have to do another route entry.

Ah, if that weren't easy enough?? I also want to set up an array, where if the name is in their, it would route those controllers normally overriding the automatic class removal.

Let me give you an idea of what I'm talking about...

Only problem with it is I get a blank page, no errors, no output, no nothing.

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

/*
$installed_controllers = array();
*/

$exclude = array('.', '..', 'index.html', 'site.php');
$dir = APPPATH.'controllers/';
$handle = opendir($dir);

// lets make this work --

$handle = opendir($dir);

while(false !== ($file = readdir($handle)))
{
    
    if (!in_array($file, $exclude))
    {
    
        $installed_controllers[] = $file;
            
    }
    
}

closedir($handle);

// testing, remove once i get output
die(print_r($installed_controllers))

foreach($installed_controllers as $control)
{
    
    $route[$control]                    = $control;
    $route[$control.'/(.*)']    = $control.'/$1';
    
}

$route['(.*)'] = "site/$1";
#2

[eluser]Mark LaDoux[/eluser]
found my issue, forgot to terminate a line.

// finished it up...

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

// automatic routing script

$exclude = array('.', '..', 'index.html', 'site.php');
$dir = APPPATH.'controllers/';
$handle = opendir($dir);

// lets make this work --

while(false !== ($file = readdir($handle)))
{
    
    if(!in_array($file, $exclude))
    {
        
        if(!is_dir(APPPATH.'controllers/'.$file))
        {
            
            $file = substr($file, 0, strlen($file) -4);
            
        }
        $installed_controllers[] = $file;

    }
    
}

closedir($handle);

// testing, remove once i get output
//die(print_r($installed_controllers));

foreach($installed_controllers as $control)
{
    
    $route[$control]                    = $control;
    $route[$control.'/(.*)']    = $control.'/$1';
    
}

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

oh, don't credit me with this, i had some help.
#3

[eluser]Mark LaDoux[/eluser]
Improved version

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
|     example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
|    http://ellislab.com/codeigniter/user-guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There are two reserved routes:
|
|    $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
|    $route['scaffolding_trigger'] = 'scaffolding';
|
| This route lets you set a "secret" word that will trigger the
| scaffolding feature for added security. Note: Scaffolding must be
| enabled in the controller in which you intend to use it.   The reserved
| routes must come before any wildcard or regular expression routes.
|
*/

$route['default_controller'] = "site";
$route['scaffolding_trigger'] = "";

/*
| Automatic Routing Script    
*/

// Exclusion Array
$exclude    = array('.', '..', 'index.html', $route['default_controller'].EXT);

// Get Directory Contents
$dir            = APPPATH.'controllers/';
$handle        = opendir($dir);

// Processing

while(false !== ($file = readdir($handle)))
{
    
    // Filter out excluded files
    if(!in_array($file, $exclude))
    {
        
        // Filter out directories
        if(!is_dir(APPPATH.'controllers/'.$file))
        {
            
            // Strip of file extension
            $file    = substr($file, 0, strlen($file) -4);
            
        }
        
        // Process Directories
        $installed_controllers[]    = $file;
        
    }
    
}

// close directory
closedir($handle);

/* Test Filters *

die(print_r($installed_controllers));

/**/

// See if controller is installed

foreach($installed_controllers as $control)
{
    
    // set route
    $route[$control]                    = $control;
    
    // process queries
    $route[$control.'/(.*)']    = $control.'/$1';
    
}

// Set default route
$route['(.*)']    = $route['default_controller']."/$1";

/* End of file routes.php */
/* Location: ./system/application/config/routes.php */
#4

[eluser]Mark LaDoux[/eluser]
simplified, but not really changed...
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
|     example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
|    http://ellislab.com/codeigniter/user-guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There are two reserved routes:
|
|    $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
|    $route['scaffolding_trigger'] = 'scaffolding';
|
| This route lets you set a "secret" word that will trigger the
| scaffolding feature for added security. Note: Scaffolding must be
| enabled in the controller in which you intend to use it.   The reserved
| routes must come before any wildcard or regular expression routes.
|
*/

$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";

/*
| -------------------------------------------------------------------------
| AUTOMATIC ROUTING SCRIPT
| -------------------------------------------------------------------------
|
| If controller does not exist, we automatically pass arguments to the default
| controller.
*/

// exclusion array
$exclude  = array('.', '..', $route['default_controller'].EXT);

// open directory
$dir    = APPPATH.'controllers/';
$handle = opendir($dir);

// Process directory contents
while ( FALSE !== ( $file = readdir($handle)))
{
  // filter excluded files
  if( ! in_array($file, $exclude))
  {
    // strip extension from files
    if( ! is_dir($dir.$file)) $file = substr($file, 0, strlen($file) - 4);
    
    // add to installed array
    $installed_controllers[]  = $file;
  }
}

// close directory
closedir($handle);

// if controller is installed, run it
foreach($installed_conrollers as $control)
{
  // set route
  $route[$control]          = $control;
  $route[$control.'/(.*)']  = $control."/$1";
}

// else use default controller
$route['(.*)']  = $route['default_controller']."/$1";

/* End of file routes.php */
/* Location: ./system/application/config/routes.php */




Theme © iAndrew 2016 - Forum software by © MyBB