Welcome Guest, Not a member yet? Register   Sign In
Visual Management of routing + routing to an external source
#1

[eluser]LiorBroshi[/eluser]
I want to give my user's the ability to manage their own routes without saving the values to the database, therefore, I've created a screen which allows the users to manage their own routing. (image attached)

This is saved to a file under third_party/ directory and then merged into the main $route array with the code inside my routes.php file:
Code:
$route['default_controller'] = "welcome";
$route['404_override'] = '';

/*
|--------------------------------------------------------------------------
| Hard Coded Routes
|--------------------------------------------------------------------------
*/

# Internal / Hard coded routes come here

/*
|--------------------------------------------------------------------------
| Custom & Dynamic Routes
|--------------------------------------------------------------------------
*/
require_once( APPPATH . 'third_party/custom_routes.php' );
if ( isset($custom_routes) && is_array($custom_routes) )
{
$route = array_merge($custom_routes, $route);

# Check for a custom 301 route
$current_uri_request = ltrim($_SERVER['REQUEST_URI'],'/');
if ( array_key_exists($current_uri_request,$custom_routes) )
{
  # Is this routing is an external one?
  if
  (
   strpos($custom_routes[$current_uri_request],'http://') !== FALSE OR
   strpos($custom_routes[$current_uri_request],'www.') !== FALSE
  )
  {
   # Make sure we have a http:// prefix
   if ( strpos($custom_routes[$current_uri_request],'http://') == FALSE )
   {
    $custom_routes[$current_uri_request] = 'http://' . $custom_routes[$current_uri_request];
   }

   header("Location: $custom_routes[$current_uri_request]");
  }
}
}

This small workaround should enable 2 features which (AFAIK) does not exists in CI:
1. Allow easy management of $route values for a non-programmer user in the project.
2. Allow routing of website urls to external sources, not only internal controllers or places inside the same project.

The problem is that when executing the `header` function, it shows a CI 404 page which I cannot understand why! any ideas?
#2

[eluser]LiorBroshi[/eluser]
Anyone?
#3

[eluser]TheFuzzy0ne[/eluser]
Have you confirmed that the code is actually being executed? I suspect that it's not, which is why CodeIgniter is trying to handle the request.

Also, I have one extra comment. Are you validating your routes somehow? If someone enters a route incorrectly, it could break your entire site.
#4

[eluser]LiorBroshi[/eluser]
Problem Solved!
I just forgot to add die() after the header() executing to executing the rest of CI.

About your second comment, the file third_party/custom_routes.php is being written by one of my controllers when the user clicks `save` on on of the views and it is being validated for correct structure to avoid php fatal errors.

Here is the final code (with some more documentation):

Code:
require_once( APPPATH . 'third_party/custom_routes.php' );
if ( isset($custom_routes) && is_array($custom_routes) )
{
# Merge my custom routes with the $route var
$route = array_merge($custom_routes, $route);

# Check for a custom 301 route
$current_uri_request = ltrim($_SERVER['REQUEST_URI'],'/');

# Current URI request exists in our custom routes?
if ( array_key_exists($current_uri_request,$custom_routes) )
{
  # Yes
  
  # Is our custom routing redirects to an external website?
  if
  (
   strpos($custom_routes[$current_uri_request],'http://') !== FALSE OR
   strpos($custom_routes[$current_uri_request],'www.') !== FALSE
  )
  {
   # Make sure we have a http:// prefix
   if ( strpos($custom_routes[$current_uri_request],'http://') == FALSE )
   {
    $custom_routes[$current_uri_request] = 'http://' . $custom_routes[$current_uri_request];
   }
   header("Location: $custom_routes[$current_uri_request]");
  
   # Stop executing!
   die();
  }
}
}




Theme © iAndrew 2016 - Forum software by © MyBB