Welcome Guest, Not a member yet? Register   Sign In
Routes: how to get mycontroller/id function same as mycontroller/action/id
#1

[eluser]dreamynsx[/eluser]
Hello, I've read routes documentation already, but it really does not have a lot of examples for what I'm trying to do. Please help.

Code:
// my sample class with index action
class MyController extends Controller
{
    function index($id="")
    {
        // do fun things here
    }

    function add() {
       // do more fun things here
    }

}


How do I get it so that both requests like this work:

www.site.com/mycontroller/index/3<-- this works by default, sets $id value to 3

www.site.com/mycontroller/3 <-- this fails with 404, since '3' is not valid action

www.site.com/mycontroller/add <-- this works as expected

www.site.com/mycontroller <-- this will work and load index action by default


This is simply because I don't want to use url like www.site.com/cars/index/3 as I think having index at the end of every section looks funny. So www.site.com/cars/3 is what I want to work.


Thanks!
#2

[eluser]Majd Taby[/eluser]
From the user guide:

$route['products/([a-z]+)/(\d+)'] = "$1/id_$2";

Which means, you probably want:

$route['cars/[0-9]'] = "cars/index/$1";
#3

[eluser]dreamynsx[/eluser]
Hi Newbie...thanks.

$rote['cars/[0-9]' = "cars/index/$1";

This works such that the page is called correctly, but the $id digit isn't initialized. Its empty.
#4

[eluser]marcoss[/eluser]
[quote author="dreamynsx" date="1192829171"]
Code:
// my sample class with index action
class MyController extends Controller
{
    function index($id="")
    {
        // do fun things here
    }

    function add() {
       // do more fun things here
    }

}
[/quote]

You should rewrite that like this,

Code:
class MyController extends Controller
{
    function index()
    {
        // load home
    }

    function id($id=0) {
       // get record by $id
    }

}

Then you get http://site.com/cars/id/3.
#5

[eluser]dreamynsx[/eluser]
Well the real idea for all this is that my index action will show a list of cars, and can accept a category

Lets say I have 20 categories. I wouldn't want to create 20 different methods just to support this.

So back to the function index($cat="") { ... }

www.site.com/cars/index/sports <-- works (shows 'sport' cars)

www.site.com/cars <-- works (just show default list)

www.site.com/cars/sports <-- fails ...
#6

[eluser]Majd Taby[/eluser]
Here's a conversation that happened in the irc channel (#codeigniter on irc.freenode.net), thought it would help clear things up:

Quote:<1 - needs help> man it must just be me, but ive never gotten even the simplest routes to work
<1 - needs help> $route["org/[0-9]"] = "org/index/$1"; ... for example
<2 - helping> $route['(org/[0-9]+)']
<2 - helping> possibly
<2 - helping> you gotta have parenthesis
<1 - needs help> aha!
<1 - needs help> funny none of the docs i found use those
<1 - needs help> but yet it works now
<1 - needs help> oops nm no it doesnt
* fio__ ([email protected]) has left #codeigniter
<1 - needs help> do i have to have function index() or function index($id=0) in my controller?
<2 - helping> neither will work in CI (because CI's routing is busted)
<2 - helping> you need $this->uri->rsegment(1)
<2 - helping> or similar
<1 - needs help> hmm, but ... if you come to the page as controller/index/id ... its segment(3) ... whereas ... controller/id ... is segment(2)
<1 - needs help> is ther e a standard for knowing how the controlerl was called
<1 - needs help> *bah fingers*
* #chemistry ##chemistry :Forwarding to another channel
<2 - helping> you could always use rsegment()
<1 - needs help> *looks that up*
<2 - helping> that's one way of being *real* sure
<2 - helping> the segment always refers to the segment, as it is in the URI
* Antivanity has quit (". . . . . http://www.yumsearch.com . . . . .")
<2 - helping> the rsegment refers to the routed segment, eg: '(foo/[0-9]+)' = 'foo/bar/$1'
<1 - needs help> hmmmmmmmmm
<2 - helping> the segment(2) would be the number
<2 - helping> and the rsegment(2) would be 'bar'
<2 - helping> if there is no route, segment === rsegment
<1 - needs help> so it essentially puts /index/ back into the url for means of coutning uri segments?
<2 - helping> in your example, yah
<1 - needs help> cool beans
<1 - needs help> and if you access foo/bar/id directly ... is rsegment(3) ... id
<1 - needs help> suppose i can check heh
<2 - helping> yes
* Antivanity ([email protected]) has joined #codeigniter
<1 - needs help> hmmm, in foo/id ... id is rsegment(4) ... but in ... foo/index/id ... id is rsegment(3)
<1 - needs help> even wierder .... in /foo/id .... rsegment(1)==foo ... rsegment(2)==index ... rsegment(3)==foo ... rsegment(4)==id
* Char80|w is now known as Char80
<1 - needs help> thats whack!
<2 - helping> interesting
<2 - helping> CI got bugs!
<2 - helping> Wink
<1 - needs help> hahah
<2 - helping> i had to rewrite Router completely when i did Kohana
<2 - helping> like... 100% re-done
<1 - needs help> so its foo/index/foo/id ... for some reason
<2 - helping> it was horrible
<2 - helping> oh, well look at the route
<2 - helping> => 'foo/index/$1
* champs|work has quit ("manamana.")
<2 - helping> and the first part: '(foo/[0-9]+)'
<2 - helping> is capturing the whole thing
<2 - helping> so, if you want just the ID transposed, you need: 'foo/([0-9]+)' => 'foo/index/$1'
<1 - needs help> without the (
<1 - needs help> ?
<2 - helping> no
<1 - needs help> i have $route["(org/[0-9]+)"] = "org/index/$1"; already
<2 - helping> in regex, (this will be captured)
<2 - helping> anything inside of parens is assigned to the "callback"
<1 - needs help> oh
<1 - needs help> so remove them then
<2 - helping> so, (one)/two/(three), $1 = one, $2 = three
<1 - needs help> got it
<1 - needs help> so i really want ... $route["org/([0-9]+)"]
<2 - helping> yes
<1 - needs help> never understood that, thanks Smile
<2 - helping> regex is a programming language all in itself
<2 - helping> Big Grin
<1 - needs help> yes, yes it is
<1 - needs help> i have used it for very simple things
<1 - needs help> but not much else
<2 - helping> <1 - needs help>: there's always fun like this: http://kohanaphp.com/trac/browser/trunk/...8.php#L678
<1 - needs help> bahahahahahah
<1 - needs help> i'll get right on that! Smile
<2 - helping> lol
<1 - needs help> *jumps up and down: routes work!*
#7

[eluser]marcoss[/eluser]
[quote author="dreamynsx" date="1192840619"]Well the real idea for all this is that my index action will show a list of cars, and can accept a category

Lets say I have 20 categories. I wouldn't want to create 20 different methods just to support this.

So back to the function index($cat="") { ... }

www.site.com/cars/index/sports <-- works (shows 'sport' cars)

www.site.com/cars <-- works (just show default list)

www.site.com/cars/sports <-- fails ...[/quote]

Again, you are missing the point here, it is about architecture, you should do something like this

Code:
class MyController extends Controller
{
    function index()
    {
        // load home
    }

    function show($cat=0,$id=0) {
       if($id) // get record by $id in $cat
       else    // get all records in $cat
    }

}

Now you have,

http://site.com/cars/show/sports --&gt; will list all cars on sports category.
http://site.com/cars/show/sports/5 --&gt; will get the car with id 5 in the sports category.
#8

[eluser]dreamynsx[/eluser]
[quote author="marcoss" date="1192921895"]

Now you have,

http://site.com/cars/show/sports --&gt; will list all cars on sports category.
http://site.com/cars/show/sports/5 --&gt; will get the car with id 5 in the sports category.[/quote]

Actually I think your not understanding my full scenario, however your example should help me make it clearer. What if in your example, I want site.com/cars to show the list of all cars, and site.com/cars/sports to show list of all sports cars ? is this not possible without having the method 'show' in there?

I do not want to have to do http://site.com/cars/show/sports. That extra show path seems uneccessary to me.
#9

[eluser]heha[/eluser]
I'm facing the same problem wtih you, dreamynsx. But in Zaatar's reply has already showed the way to complete that.

Try:
$route['(cars/[a-z]+)'] = "cars/index/$1";
(Close route with bracket.)
That's it.

Anyway I still don't know why this work. Don't know very much with regular expression.
#10

[eluser]easymind[/eluser]
http://xxx.xx/cars/1
runs the function called 1 inside your controller cars
(so it will not start the index function and pass 1 as a argument)

http://xxx.xx/cars/index/1
runs the index function inside your controller cars and passes 1 as the first argument

As suggested to you, this can be done nicer by making a function called show, so you can leave out index in the url and replace it with show. Show will have to do what you want index to do.
So you will get the url:
http://xxx.xx/cars/show/1

The other suggestion was routing. So cars/1 goes to cars/index/1 or cars/show/1, whatever you like...

Is this a simpler explanation of the suggested solutions above?




Theme © iAndrew 2016 - Forum software by © MyBB