Welcome Guest, Not a member yet? Register   Sign In
Pass agrument to home page
#1

[eluser]thephpguy[/eluser]
So here is what i want to do,

I am developing a small project,
Code:
http://abc.com/

what i want is if user enter
Code:
http://abc.com/username
they should see the profile or any page which will describe about the user
Code:
'username'
this is what i think is like passing argument to home page or more specifically to the default controllers index function.

This can be achieved By simply changing the url to http://abc.com/controller/index/username this will work but the url seems very clumsy its no longer pretty which i want. I tried using _remap but it doesnt works. its gives me 404 error i know its looking for a controller 'username' which doesnot exists.

So what i want, is there any way by which we can ask CI to consider this kinds of url as special urls i mean we can route this urls to any specific controllers. I am not regex expert but i know this can also be done using routing using regex. If you can it will be very big help for me.

Thanks in advance ;-)
#2

[eluser]smilie[/eluser]
Look at routes.php. I still strugle with those myself, so can not give you any concrete answers Sad

Cheers,
Smilie
#3

[eluser]Cristian Gilè[/eluser]
Code:
$route['(:any)'] = "controller/method/$1";
#4

[eluser]thephpguy[/eluser]
[quote author="Cristian Gilè" date="1294272314"]
Code:
$route['(:any)'] = "controller/method/$1";
[/quote]

I tried but wont help, it will redirect all requests to controller/method which i dont want. Any other suggestion??? 8-/
#5

[eluser]sean_THR[/eluser]
Replace "controller/method" with the actual name of the controller and method that you want to call.
#6

[eluser]thephpguy[/eluser]
[quote author="sean_THR" date="1294315766"]Replace "controller/method" with the actual name of the controller and method that you want to call.[/quote]

Yea but what to route??
Code:
$route['(:any)']
will route all the requests which i dont want..
#7

[eluser]Atharva[/eluser]
Code:
$route['^(?!controller1|controller2|controller3).*'] = "yourcontroller/yourfunction/";

What this will do is this will redirect domain.com/username to yourcontroller/yourfunction. It will check if the controller in the uri is NOT the one mentioned in the rule. So you can have a special controller only for handling the 'domain.com/username' link structure.
#8

[eluser]thephpguy[/eluser]
[quote author="Atharva" date="1294316412"]
Code:
$route['^(?!controller1|controller2|controller3).*'] = "yourcontroller/yourfunction/";

What this will do is this will redirect domain.com/username to yourcontroller/yourfunction. It will check if the controller in the uri is NOT the one mentioned in the rule. So you can have a special controller only for handling the 'domain.com/username' link structure.[/quote]

Hmmm will try that but dont you think it will be quite tedious to keep track of all controllers and add them up. Its good no doubt but how about this check the URL for ex:
Code:
http://abc.com/username
So check what ever follows after
Code:
http://abc.com/
check that does that contains any
Code:
'/'
if yes then its a normal url and dont do anything but if its not then route it to
Code:
mycontroller/myfunction/$1
as i said i suck with regex but this is very simple with regex.
#9

[eluser]sean_THR[/eluser]
This is a pretty lazy untested regex:
Code:
^(.+)\/(.+)$
From the beginning of the string, it will match any character one or more times until it finds a slash, and then it will match any character one or more times(again, but this time it will include slashes) until the end of the string.

So, in theory, http://www.abc.com/username will not produce a match, but http://www.abc.com/controller/method will as will http://www.abc.com/controller/method/paramater.

Anyway, I think it should do what you need, but it may need work. Once again, I have not tested it.
#10

[eluser]thephpguy[/eluser]
[quote author="sean_THR" date="1294319498"]This is a pretty lazy untested regex:
Code:
^(.+)\/(.+)$
From the beginning of the string, it will match any character one or more times until it finds a slash, and then it will match any character one or more times(again, but this time it will include slashes) until the end of the string.

So, in theory, http://www.abc.com/username will not produce a match, but http://www.abc.com/controller/method will as will http://www.abc.com/controller/method/paramater.

Anyway, I think it should do what you need, but it may need work. Once again, I have not tested it.[/quote]

Excellent, but can you get something to do opposite, what i mean is any regex to match
Code:
http://www.abc.com/username
but not
Code:
http://www.abc.com/controller/method

And how do you use regex in routes?? i have been trying but cant get it working :-(




Theme © iAndrew 2016 - Forum software by © MyBB