Welcome Guest, Not a member yet? Register   Sign In
How can I control the acces to a page using routing and uri with a special behavior?
#1

[eluser]Optimus Prime[/eluser]
Hi!

I post this topic because I want to know your opinion about my problem and to know another alternatives for it before to write a large code. I tested some alternatives but I want to know if there another way to acommplish my goal.

I explain my problem:

- Suppose that I have many controllers, all of them have the "show" function and it uses a parameter "name".
- Suppose that doesn't exist the same name between the sections (or controllers, in this case).

An example is:

site.com/people/show/name
site.com/places/show/name
site.com/{others_controller}/show/name

So, the problem is that I want to create easy access for the visitors (shorts url). I mean that redirect to the full address . Ideally, I want two types for accesing (or to redirect to) a page. I show it as an example:

Redirection Type 1:

site.com/name --> redirects to --> site.com/people/show/name

and

Redirection Type 2:

site.com/people/name --> redirects to --> site.com/people/show/name

The redirection type 2 can be discarded, but at less I need the type 1.

An alternative that I tried is to make a function (named redirection) that search for the name and redirect to the right controller (or URI adress) and config the routes (config/routes.php) in this way (based in the previos example):


$route['people(.*)'] = "people$1";
$route['places(.*)'] = "place$1";
$route['{other_controller}(.*)'] = "{other_controller}$1";
...etc...

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

It work for Redirection Type 1, but no for Redirection Type 2. I guess that I need to write the functions in $route like:

$route['people/list(.*)'] = "people/list(.*)";

So this mean that I need to config the config.php many times and write all the functions names too?(when I need to add o delete sections) a route map. Am i right? Ideally, I want to avoid it.

Another alternative but I want to avoid it, is to write a code like this:

Code:
function controller_function($action, $params...) {
  if ($action=="action1") {
  }
  elseif ($action=="action2") {
  }
  ...

}

Unless there is no way to accomplish my goal I will code with this style. But I want to find a better solution because the site that I'm working will suffer many modifications (sections will be added or deleted with the time).

The best alternative that I wish to find is to configure CI to process the uri segments: if the controller segment does not exist (in the code), before to show the error page, check if this uri segment is a name parameter and if is a name that exist in a database, then redirect to the rigth controller and function. Is this posible?

Or there is another suggestion?. I'll pleased to read your answers! :lol:




Theme © iAndrew 2016 - Forum software by © MyBB