Skip to main content
Routing maps your URL to specific controller’s action. In this section, we will see how you can implement routes, how you can pass arguments from URL to controller’s action, how you can generate URLs, and how you can redirect to a specific URL. Normally, routes are implemented in file config/routes.php. Routing can be implemented in two ways −
  • static method
  • scoped route builder
Here is an example presenting both the types.
// Using the scoped route builder.
Router::scope('/', function ($routes) {
   $routes->connect('/', ['controller' => 'Articles', 'action' => 'index']);
});

// Using the static method.
Router::connect('/', ['controller' => 'Articles', 'action' => 'index']);
Both the methods will execute the index method of ArticlesController. Out of the two methods scoped route builder gives better performance.

Connecting Routes

Router::connect() method is used to connect routes. The following is the syntax of the method −
static Cake\Routing\Router::connect($route, $defaults =[], $options =[])
There are three arguments to the Router::connect() method −
  • The first argument is for the URL template you wish to match.
  • The second argument contains default values for your route elements.
  • The third argument contains options for the route which generally contains regular expression rules.
Here is the basic format of a route −
$routes->connect(
   'URL template',
   ['default' => 'defaultValue'],
   ['option' => 'matchingRegex']
);

Example

Make changes in the config/routes.php file as shown below.
config/routes.php
<?php
   use Cake\Core\Plugin;
   use Cake\Routing\RouteBuilder;
   use Cake\Routing\Router;

   Router::defaultRouteClass('DashedRoute');
   Router::scope('/', function (RouteBuilder $routes) {
      $routes->connect('/', ['controller' => 'Tests', 'action' => 'index']);
      $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
      $routes->fallbacks('DashedRoute');
   });
   Plugin::routes();
Create a TestsController.php file at src/Controller/TestsController.php. Copy the following code in the controller file.
src/Controller/TestsController.php
<?php
   namespace App\Controller;
   use App\Controller\AppController;

   class TestsController extends AppController{
      public function index(){
      }
   }
?>
Create a folder Tests under 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
This is CakePHP tutorial and this is an example of connecting routes.
Execute the above example by visiting the following URL.
http://localhost:85/CakePHP/
The above URL will yield the following output.
Routing

Passed Arguments

Passed arguments are the arguments which are passed in the URL. These arguments can be passed to controller’s action. These passed arguments are given to your controller in three ways.

As arguments to the action method

Following example shows how we can pass arguments to the action of the controller.
Visit the following URL − http://localhost:85/CakePHP/tests/value1/value2
This will match the following route line.
$routes->connect('tests/:arg1/:arg2', ['controller' => 'Tests', 'action' =>
   'index'],['pass' => ['arg1', 'arg2']]);
Here the value1 from URL will be assigned to arg1 and value2 will 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 statement.
$args = $this->request->params[‘pass’]
The arguments passed to controller’s action will be stored in $args variable.

Using routing array

The argument can also be passed to action by the following statement −
$routes->connect('/', ['controller' => 'Tests', 'action' => 'index',5,6]);
The above statement will pass two arguments 5, and 6 to TestController’s index() method.

Example

Make Changes in the config/routes.php file as shown in the following program.
config/routes.php
<?php
   use Cake\Core\Plugin;
   use Cake\Routing\RouteBuilder;
   use Cake\Routing\Router;

   Router::defaultRouteClass('DashedRoute');
   Router::scope('/', function (RouteBuilder $routes) {
      $routes->connect('tests/:arg1/:arg2', ['controller' => 'Tests', 'action'=> 
         'index'],['pass' =>['arg1', 'arg2']]);
      
      $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
      $routes->fallbacks('DashedRoute');
   });

   Plugin::routes();
Create a TestsController.php file at src/Controller/TestsController.php. Copy the following code in the controller file.
src/Controller/TestsController.php
<?php
   namespace App\Controller;
   use App\Controller\AppController;

   class TestsController extends AppController{
      public function index($arg1,$arg2){
         $this->set('argument1',$arg1);
         $this->set('argument2',$arg2);
      }
   }
?>
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
This is CakePHP tutorial and this is an example of Passed arguments.<br />
Argument-1: <?=$argument1?><br />
Argument-2: <?=$argument2?><br />
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.

Comments

Popular posts from this blog

Virtualization is one of the most important technology feature and Microsoft has invested on this and the responsible role is called as Hyper-V. Let us now see how to install the Hyper-V Role, for doing this we should follow the steps given below. Step 1  − To Install DNS role go to “Server Manager” → Manage → Add Roles and Features. Step 2  − Click Next. Step 3  − Select the  Role-based  or  feature-based installation  option → click Next. Step 4  − I will install a Local Hyper-V role as it will Select a server from the server pool → Next. Step 5  − From the Roles lists, check the Hyper-V Server role → click Add Features on the popup windows which show up and then → Next. Step 6  − Click Next. Step 7  − Choose your server’s physical network adapters that will take part in the virtualization → Next. Step 8  − Under  Migration , leave the default settings →  Next . Step 9  − Choose the...
The Windows Firewall with Advanced Security is a firewall that runs on the Windows Server 2012 and is turned on by default. The Firewall settings within Windows Server 2012 are managed from within the  Windows Firewall Microsoft Management Console . To set Firewall settings perform the following steps − Step 1  − Click on the Server Manager from the task bar → Click the Tools menu and select Windows Firewall with Advanced Security. Step 2  − To see the current configuration settings by selecting  Windows Firewall Properties  from the MMC. This  allows access to modify the settings  for each of the three firewall profiles, which are –  Domain, Private and Public  and IPsec settings. Step 3  − Applying custom rules, which will include the following two steps − Select either  Inbound Rules  or  Outbound Rules  under  Windows Firewall with Advanced Security  on the left side of the management console...

HaydarOrac