views:

81

answers:

2

Hi all,

I am using cakephp v1.26.
I got a function in a controller like this:

class testingsController extends AppController{

function testing($id=null){
$recieved = $id;}

}

I am not sure if there are any better ways to pass a parameter to the Action testing.
But I have come across some web sites and got these two methods.
Is there any difference in the following parameter passing methods?

1. url/testings/testing/1
2. url/testings/testing:1
+3  A: 

url/testings/testing/1

With standard routes, this will call TestingsController::testing(1).

This is standard parameter passing, any parameters beyond /:controller/:action/ are passed "as-is" to the called action.

/controllers/action/param1/param2 corresponds to
ControllersController::action($param1, $param2)

url/testings/testing:1

With standard routes, this will call TestingsController::index() and
set $this->params['named']['testing'] to 1. This is known as a named parameter.

Named parameters can be passed in any order. These two URLs are equivalent:
url/testings/testing:1/foo:2
url/testings/foo:2/testing:1

They will not be passed to the function, as in function testing($id = null). $id will be null. They're only available in the $this->params['named'] array.

deceze
Yes, a far more verbose answer than mine, and I'd forgotten [named]! Shame on me ;)
DavidYell
+1  A: 

The first example you have will pass it as a numeric parameter

$this->params[0]; // 1

The second will pass a named pair, rather like an array

$this->params['testing']; // 1

You can use either for different things. You'll notice that the paginator uses key:val paired parameters when sorting columns and pages.

There is a little bit of further info in the Book, http://book.cakephp.org/view/543/Passing-parameters-to-action

DavidYell