CakePHP Routing - CakePHP

What is CakePHP Routing?

Routing maps your URL to unique controller’s movement. on this phase, we are able to see how you can put in force routes, how you may skip arguments from URL to controller’s action, how you may generate URLs, and the way you can redirect to a specific URL. usually, routes are carried out in record config/routes.php. Routing can be applied in two ways

  • static method
  • scoped path builder

Here is an instance imparting each the sorts.

Each the techniques will execute the index approach of ArticlesController. Out of the two methods scoped path builder offers better overall performance.

Connecting Routes

Router::join() approach is used to connect routes. the following is the syntax of the technique –

There are 3 arguments to the Router::connect() method −

  • The primary argument is for the URL template you wish to healthy.
  • The second one argument consists of default values in your path elements.
  • The third argument incorporates options for the direction which commonly consists of normal expression guidelines.

Here is the fundamental format of a path −

Instance:

Make modifications within the config/routes.php document as proven under.

config/routes.php

Create a TestsController.php document at src/Controller/TestsController.php. copy the subsequent code in the controller document.

Create a folder tests under src/Template and beneath that folder create a View file called index.ctp. replica the following code in that file.

src/Template/Tests/index.ctp

Complete the above instance by visiting the next URL.

http://localhost:85/CakePHP/

The above URL will yield the subsequent output.

local host

Exceeded Arguments

Exceeded arguments are the arguments which are surpassed in the URL. these arguments may be surpassed to controller’s movement. those passed arguments are given to your controller in 3 ways.

As arguments to the movement approach

Following instance suggests how we can pass arguments to the action of the controller.

Go to the subsequent URL − http://localhost:85/CakePHP/tests/value1/value2

This can suit the following path line.

Here the value1 from URL can be assigned to arg1 and value2 could be assigned to arg2.

As Numerically Indexed Array

Once the argument is passed to the controller’s action, you can get the argument with the following assertion.

The arguments surpassed to controller’s movement will be stored in $args variable.

Using Routing Array

The argument can also be exceeded to movement through the subsequent statement −

The above announcement will skip two arguments 5, and six to TestController’s index() technique.

Instance

Make changes within the config/routes.php document as proven in the following program.
config/routes.php

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

src/Controller/TestsController.php

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

src/Template/Tests/index.ctp

Execute the above example by visiting the following URL.

http://localhost:85/CakePHP/tests/Virat/Kunal

Upon execution, the above URL will produce the following output.

localhost2

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

CakePHP Topics