CakePHP – Generating URLs

CakePHP – Generating URLs

This is a cool feature of CakePHP. Using the generated URLs, we can easily change the structure of URL in the application without modifying the whole code.

url( string|array|null $url null , boolean $full false )

The above function will take two arguments −

  • The first argument is an array specifying any of the following − ‘controller’, ‘action’, ‘plugin’. Additionally, you can provide routed elements or query string parameters. If string, it can be given the name of any valid url string.
  • If true, the full base URL will be prepended to the result. Default is false.


Make Changes in the config/routes.php file as shown in the following program.


   use CakeCorePlugin;
   use CakeRoutingRouteBuilder;
   use CakeRoutingRouter;

   Router::scope('/', function (RouteBuilder $routes){


Create a GeneratesController.php file at src/Controller/GeneratesController.php. Copy the following code in the controller file.


   namespace AppController;
   use AppControllerAppController;
   use CakeORMTableRegistry;
   use CakeDatasourceConnectionManager;

   class GeneratesController extends AppController{
      public function index(){

Create a folder Generates at src/Template and under that folder create a View file called index.ctp. Copy the following code in that file.


This is CakePHP tutorial and this is an example of Generating URLs.

Execute the above example by visiting the following URL −


The above URL will produce the following output −


Was this article helpful?

Related Articles

Leave A Comment?

You must be logged in to post a comment.