Welcome Guest, Not a member yet? Register   Sign In
Routing with regular expression - help please
#1

[eluser]umagrama[/eluser]
Hello. I've been trying to resolve a regex issue for a couple of days, this community is the last resort to it... dont know what else to do. And how difficult is to find a good resource on regex... found some stuff here, some stuff there. Nothing really complete, from A to Z, you know.

Anyway... here's my little issue that's bugging my days.

Obective::

when:

1) user access www.example.com/users/page, great, user is directed there.

OR

2) user access wwww.example.com/anything_else, then anything_else is sent to link_controller/check_uri/anything_else

Possible solution::

$route['u(?!ser)|(?<!u)s(?!er)|(?<us)er|[^user]'] = "link/check_uri/$1";

I've read articles, tried different things, did this and that... I can't get around this one. Any idea on how to accomplish this?

Thanks ahead! Happy black friday!
#2

[eluser]CroNiX[/eluser]
Code:
$route['users/page'] = 'users/page';
//enter additional routes here
$route['(:any)'] = 'link_controller/check_uri/$1';  //anything besides users/page will be sent here.  This should be the very last route.  Any other routes should come before this one

Code:
function check_uri($page = '')
{
  //$page will contain whatever the original url segment was, so if you go to www.yoursite.com/foo, $page would be 'foo'.
}
#3

[eluser]umagrama[/eluser]
Wow... brilliant! Best solutions are so simple. Thanks CroNiX! I am going to try it.

Here I was messing around with regex... overkill.
#4

[eluser]CroNiX[/eluser]
Once you have a catch-all route (the last one I made containing only one segment), it complicates things because then you need to create a route for everything else or it will go to the catch-all.

If instead, you had it say "content/the-url", it would be much simpler and you wouldn't need to create a route for everything else because it would only get triggered if "content" was the first segment (or whatever you want to call it). But then you'd always have to have "content" as a segment in the url, which isn't quite as clean but makes the code much easier.

Then you could just have
Code:
$route['content/(:any)'] = 'link_controller/check_uri/$1';

So if you had www.yoursite.com/content/some-article

It would send "some-article" to check_uri(), and you wouldn't need any additional routes unless you want to.
#5

[eluser]umagrama[/eluser]
Yeah... I thought about having a "/content/..." to distinguish between existing content and everything else. But since this little project will only have 3-4 controllers I will define their routing explicitly in routes.php.

This is a small project, first one in fact using CI, I'm creating a URL shortener for shits and giggles with user registration, links control, maybe some charts. Before CI I'd use htaccess for the redirection. It's so much easier and faster with CI once you get the hang of it.

Thanks for helping me out and clearing stuff up. I love it.






Theme © iAndrew 2016 - Forum software by © MyBB