CakePHP 4 How To Redirect To Other Routes Tutorial

Share this Article
Reading Time: 5 minutes
485 Views

Inside this article we will see the concept i.e CakePHP 4 How to redirect to other routes. We define routes for CakePHP application via routes.php file from /config folder.

Article contains classified information about redirecting to different route from cakephp 4 actions. CakePHP provides redirect() method where we can set different route of application according to redirection.

According to CakePHP 4 document we can redirect to a different location by means of few ways –

  • Proving the information about controller and action
  • Using redirect URL to redirect() method.

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.

Redirection To Routes

As per the CakePHP document we can do redirection via few methods. Let’s understand this concept by creating some routes.

Open routes.php from /config folder and these routes into it.

//...

$routes->connect(
    '/single-product/{id}',
    ['controller' => 'Site', 'action' => 'getSingleProduct']
)->setPass(["id"]);

$routes->connect(
    '/add-product',
    ['controller' => 'Site', 'action' => 'addProduct']
);

$routes->connect(
    '/list-products',
    ['controller' => 'Site', 'action' => 'listAllProducts']
);

//...

All above routes using SiteController.php and few actions used like getSingleProduct(), addProduct() and listAllProducts().

To do redirection in application from one location to other we use redirect() method.

Syntax

public function redirect($url, int $status = 302){}

By default 302 is the status of redirection. We can according to the need like 301, 303, etc.

Let’s see how to redirect –

Using redirect URL to redirect() method

Here,

We are redirecting from addProduct() method to list listAllProducts().

<?php

declare(strict_types=1);

namespace App\Controller;

class SiteController extends AppController
{
    // Add product
    public function addProduct()
    {
        // Code to add product

        // Redirect to list products route
        return $this->redirect("/list-products");
    }

    public function listAllProducts()
    {
        // List products
    }
}

Concept

return $this->redirect("/list-products");

Proving the information about controller and action

Here,

Instead of passing URL to redirect we can pass the information of controller, action, etc.

<?php

declare(strict_types=1);

namespace App\Controller;

class SiteController extends AppController
{
    // Add product
    public function addProduct()
    {
        // Code to add product

        // Redirect to get single product route
        return $this->redirect([
            "controller" => "Site",
            "action" => "getSingleProduct",
            12 // product id
        ]);
    }

    public function getSingleProduct()
    {
        // Get single product
        $id = $this->request->getParam("id"); // access "id" from URL
    }

    public function listAllProducts()
    {
        // List products
    }
}

Concept

// Redirect to get single product route
return $this->redirect([
    "controller" => "Site",
    "action" => "getSingleProduct",
    12 // product id
]);

Additionally we can pass parameters, query string values, etc with redirection. Have a look the complete setup for redirection as –

return $this->redirect([
    'controller' => 'Site',
    'action' => 'anyMethod',
    20,
    '?' => [
        'product' => 'sample',
        'quantity' => 3
    ],
    '#' => 'top'
])

We hope this article helped you to learn about CakePHP 4 How To Redirect To Other Routes Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5