Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Routing setter/tester
  • Here is FuelPHP routing setter/tester. This is one control class to set and test your route.php setting. It is not beautiful code and no exact error check, but it is for developer, not for general users and your customer to show. Notice!
    This class write over you fuel/app/config/route.php. It means your comments will be vanished. So you must backup if you needed. The best way to start to use this, try this on new installed FuelPHP to understand FuelPHP routing and this program. (Yes, may be still have small bugs I don't find.)
    <?php
    
    class Controller_Routingsetter extends Controller
    {
    
     const NEW_ROUTE = 5;
    
     public function action_index()
     {
      if (Input::method() == 'POST')
      {
       // get routes had been set
       $total_line = Input::post('total_line');
       $route_array = array();
    
       for ($i = 0; $i < $total_line; $i++)
       {
        if (!(Input::post('line'.$i, false) === false) and Input::post('pattern'.$i) and Input::post('class_action'.$i) and !Input::post('delete_radio'.$i))
        {
         $route_array[Input::post('line'.$i)] = array('pattern' => Input::post('pattern'.$i), 'class_action' => Input::post('class_action'.$i));
        }
       }
    
       // get add new routing items
       for ($i = 0; $i < self::NEW_ROUTE; $i++)
       {
        if (Input::post('new_line'.$i) and Input::post('new_pattern'.$i) and Input::post('new_class_action'.$i))
        {
         $route_array[Input::post('new_line'.$i)] = array('pattern' => Input::post('new_pattern'.$i), 'class_action' => Input::post('new_class_action'.$i));
        }
       }
    
       ksort($route_array);
    
       // set config routes group
    
       Config::delete('routes');
       foreach ($route_array as $item)
       {
        Config::set('routes.'.$item['pattern'], $item['class_action']);
       }
    
       // save routes group as routes.php
       Config::save('routes', 'routes');
    
      } // End of POST handing
    
      // get config routes group
      $routes = Config::get('routes', array());
    
      $rt = array();
      foreach ($routes as $key => $item)
      {
       // transform route pattern to reguler expression
       $replace = str_replace(array(
        ':any',
        ':alnum',
        ':num',
        ':alpha',
        ':segment',
        ),
        array(
        '.+',
        '[[:alnum:]]+',
        '[[:digit:]]+',
        '[[:alpha:]]+',
        '[^/]*',
        ), '/'.$key.'/');
       $replace = preg_replace('%:([[:alpha:]]+)/%', '(?P<$1>.+?)/', $replace);
    
       $rt[] = array(
        'exp' => trim($replace, '/'),
        'pattern' => trim($key, '/'),
        'class_action' => trim($item, '/')
       );
      }
    
      // simulate routing
      $matched = false;
      $p404 = false;
      $test_uri = trim(Input::post('test_uri'), '/');
    
      foreach ($rt as $key => $item)
      {
       if ($test_uri == '' and $item['exp'] == '_root_')
       {
        $matched = $key;
        break;
       }
       elseif (preg_match('{^'.$item['exp'].'$}', $test_uri, $matches))
       {
        $matched = $key;
        break;
       }
       elseif ($item['pattern'] == '_404_')
       {
        $p404 = $key;
       }
      }
      if (!$matched)
       $matched = $p404;
    
      // output content without view
    
      echo Html::doctype('html5');
      echo '<head><meta charset="UTF-8"></head><body>';
    
      echo Form::open(Uri::current());
      echo 'Routing test URI?'.Form::input('test_uri', Input::post('test_uri') ? : '',
       array('size' => 60)).'<hr />';
    
      $last_key = -1;
      foreach ($rt as $key => $item)
      {
       $last_key = $key;
       echo Form::input('line'.$key, $key * 10, array('size' => 3)).' : ';
       echo Form::input('pattern'.$key, $item['pattern'], array('size' => 50)).'&nbsp;';
       echo Form::input('class_action'.$key, $item['class_action'],
        array('size' => 50)).'&nbsp;';
       echo Form::label('Delete', 'delete_radio'.$key);
       echo Form::radio('delete_radio'.$key, 'delete_radio'.$key).'<br />';
    
       if ($key == $matched)
       {
        echo '<div>';
        $match = true;
       }
       else
       {
        echo '<div>';
       }
    
       // escapte reguler expressions
       echo 'Compire expression : '.Security::htmlentities($item['exp']).'<br />';
    
       if ($key === $p404)
       {
        echo 'Matched as _404_';
       }
       elseif ($key == $matched)
       {
        if ($key == '_root_')
        {
         echo 'Matched _root_';
        }
        else
        {
         ksort($matches, SORT_LOCALE_STRING);
         foreach ($matches as $k => $m)
         {
          if (is_numeric($k))
          {
           echo '$'.$k.' => '.'"'.$m.'"<br />';
          }
          else
          {
           echo '$this->param("'.$k.'") => "'.$m.'"<br />';
          }
         }
        }
       }
       echo '</div><hr />';
      }
    
      // keep count items
      echo Form::hidden('total_line', $last_key + 1);
    
      // add new routes
      echo '<p>Add New route</p>';
      for ($i = 0; $i < self::NEW_ROUTE; $i++)
      {
       echo Form::input('new_line'.$i, ($last_key + $i + 1) * 10, array('size' => 3)).' : ';
       echo Form::input('new_pattern'.$i, '', array('size' => 50)).'&nbsp;';
       echo Form::input('new_class_action'.$i, '', array('size' => 50)).'<br />';
      }
      echo '<hr />';
    
      echo Form::submit('submit', 'Submit');
      echo Form::close();
    
      echo '</body>';
     }
    
    }
    

    (additon : update at 25th/Jan/2012 )

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!