Question

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
Was it helpful?

Solution

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.

OTHER TIPS

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/2.0/en/development/routing.html#passed-arguments

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top