CakePHP 4 Passing Parameters to Routes and Actions

Reading Time: 4 minutes
2,705 Views

Inside this article we will see CakePHP 4 Passing Parameters to Routes and Actions. Article contains very classified information about reading parameters values from URLs in CakePHP application.

Examples of Route Parameters are –

/4
/library-page
/101/cakephp-4

To get access over these values or to work in proper way for parameterized routing in cakephp we have a step by step process. These is some route configuration which needs to be added while creating route with parameters.

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 Route with Parameters

Open routes.php from /config folder.

//...

$routes->scope('/', function (RouteBuilder $builder) {
  
  $builder->connect(
     '/products/:id',
     ['controller' => 'Sample', 'action' => 'index'],
     ["pass" => ["id"]]
  );
}); 

//...

We can see we have added parameter into route as –

'/products/:id'

OR

'/products/{id}'

Also, we have added this id into [“pass” => [“id”]]. Alternatively we can set parameters using setPass() method.

So, we can write an alternative syntax of Route with Parameter as –

//...

$routes->scope('/', function (RouteBuilder $builder) {
  
  $builder->connect(
      '/products/{id}',
      ['controller' => 'Sample', 'action' => 'index']
  )->setPass(["id"]);
}); 

//...

Access Parameter to Actions

Inside controller actions, we will have the simple process to access the value.

<?php

declare(strict_types=1);

namespace App\Controller;

class SampleController extends AppController
{
    public function initialize(): void
    {
        parent::initialize();
    }

    public function index($id)
    {
        echo $id; // id value of Route
        // your code
    }
}

We can see we are getting $id value into index() method.

Alternative way to get access over parameter value –

echo $this->request->getParam("id");
<?php

declare(strict_types=1);

namespace App\Controller;

class SampleController extends AppController
{
    public function initialize(): void
    {
        parent::initialize();
    }

    public function index()
    {
        echo $this->request->getParam("id");
        
        // your code
    }
}

Pattern Settings for Route Parameters

In case you want to restrict the URL route values, then this concept will help you. For example – id value will only integer. So if we pass any string value in place of it, then it will give an exception like may be MissingController.

Setting URL value for Integers only

//...

$routes->scope('/', function (RouteBuilder $builder) {
  
  $builder->connect(
     '/products/{id}',
     ['controller' => 'Sample', 'action' => 'index'],
     ["pass" => ["id"], "id" => "[0-9]+"]
  );
}); 

//...

Alternative syntax to write is as –

//...

$routes->scope('/', function (RouteBuilder $builder) {
  
  $builder->connect(
     '/products/{id}',
     ['controller' => 'Sample', 'action' => 'index'],
     ["pass" => ["id"]]
  )->setPatterns(["id" => "[0-9]+"]);
}); 

//...

Passing More Route Parameters

We can extend the same syntax to pass parameters into route into a new level –

//...

$routes->scope('/', function (RouteBuilder $builder) {
  
  $builder->connect(
     '/products/{id}/{name}',
     ['controller' => 'Sample', 'action' => 'index'],
     ["pass" => ["id", "name"]]
  )->setPatterns(["id" => "[0-9]+"]);
}); 

//...

We hope this article helped you to learn about CakePHP 4 Passing Parameters to Routes and Actions 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.