Cakephp get url parameters


Cakephp get url parameters : $this->request->pass; is used to get the url parameters in cakephp. Basically Request & Response Objects are responsible for the http request and response. Here request parameter is used to get the url parameters so we are going to explain the request method. Request is default request object which is used in cakephp. By default the request object is assigned to $this->request. The $this->request is available in –

  • Controllers
  • Cells
  • Views
  • Helpers

You can access $this->request anywhere in controllers, Cells, Views Or Helpers. You can also access this is components. In this tutorial we are going to explain how to get parameters in cakephp and how to use them.


Cakephp get url parameters

Here are some main tasks which $this->requestis used to perform-

  • GET/POST and FILES Arrays.
  • Provides access to request parameters as array and object both.

Here is an syntax to get the passed arguments in url in cakephp-

Cakephp get url parameters Syntax –

// Get the Passed arguments
$this->request->pass;
$this->request['pass'];
$this->request->params['pass'];

The above syntax will give you the arguments of url passed along with controller. Now let us understand with example which will make more understanding on the above syntax.

Example

Suppose we have parameters something like www.example.com/posts/view/112/comment/id/444 we can access the parameters as below –

Cakephp get url arguments Example

//www.example.com/posts/view/112/comment/id/444 

$params = $this->params['pass'];

print_r($params);
//will give you
Array
(
    [0] => 112
    [1] => comment
    [2] => id
    [3] => 444
)

In the above example if you will print the params it will give you array as above. You can also use $this->request->pass; or $this->request->params[‘pass’]; to get the parameters. $this->request->pass; Will give the results as object and $this->request->params[‘pass’]; will give as array.

Tip for Cakephp get url parameters : You can use the above syntax to get the parameters in Controllers, Views, Cells and Helper also.

Advertisements

Add Comment

📖 Read More