CakePHP 4 How To Get Query String Parameters Tutorial

Reading Time: 3 minutes
3,634 Views

Inside this article we will see CakePHP 4 How to get query string parameters values. Article contains very classified information about reading query string values from URL.

Examples of Query string are –

?name=Sanjay&email=sanjay@gmail.com

/?mobile=1234567890&rollId=29&pId=15002&grpId=8526

Query string parameters are concatenated using & symbol starts using ? symbol.

To get over the values of query string, CakePHP 4 have few methods. After using these methods you can read all the values either in bulk or access a single value. So, if you are looking for an article which gives you the understanding of reading Query string values in CakePHP 4, then you are at right place to learn.

Learn More –

Let’s get started.

CakePHP 4 Installation

To create a CakePHP project, run this command into your shell or terminal. Make sure composer should be installed in your system.

$ composer create-project --prefer-dist cakephp/app:~4.0 mycakephp

Above command will creates a project with the name called mycakephp.

Create Controller

Open project into terminal and run this command –

$ bin/cake bake controller Site --no-actions

It will create a file SiteController.php inside /src/Controller folder.

Open SiteController.php and write this code into it.

<?php

declare(strict_types=1);

namespace App\Controller;

class SiteController extends AppController
{
    public function initialize(): void
    {
        parent::initialize();
        // Loading layout
        $this->viewBuilder()->setLayout("app");
    }

    public function index()
    {
        // Reading all query string parameters
        $queryParams = $this->request->getQueryParams();
        print_r($queryParams);
        
        // Read a single specific value from query string via key name
        echo $this->request->getQuery("name");
      
        // your code
    }
}

Get all parameters from Query string, It returns the all values into an array format.

$this->request->getQueryParams();

To get a single value from query string we use the key name, “name” is the parameter name of query string.

$this->request->getQuery("name");

Application Testing

Example of a URL

http://localhost:8765/notification?name=Sanjay&email=sanjay@gmail.com

Read all parameters

$queryParams = $this->request->getQueryParams();
print_r($queryParams);

Output

Array ( [name] => Sanjay [email] => sanjay@gmail.com )

Get a single specific value

echo $this->request->getQuery("name");

Output

Sanjay

We hope this article helped you to learn about CakePHP 4 How To Get Query String Parameters Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more