- CakePHP - Home
- CakePHP - Overview
- CakePHP - Installation
- CakePHP - Folder Structure
- CakePHP - Project Configuration
- CakePHP - Routing
- CakePHP - Controllers
- CakePHP - Views
- CakePHP - Extending Views
- CakePHP - View Elements
- CakePHP - View Events
- CakePHP - Working with Database
- CakePHP - View a Record
- CakePHP - Update a Record
- CakePHP - Delete a Record
- CakePHP - Services
- CakePHP - Errors & Exception Handling
- CakePHP - Logging
- CakePHP - Form Handling
- CakePHP - Internationalization
- CakePHP - Session Management
- CakePHP - Cookie Management
- CakePHP - Security
- CakePHP - Validation
- CakePHP - Creating Validators
- CakePHP - Pagination
- CakePHP - Date and Time
- CakePHP - File upload
- CakePHP Useful Resources
- CakePHP - Quick Guide
- CakePHP - Useful Resources
- CakePHP - Discussion
CakePHP - Redirect Routing
Redirect routing is useful when we want to inform client applications that this URL has been moved. The URL can be redirected using the following function.
static Cake\Routing\Router::redirect($route, $url, $options =[])
There are three arguments to the above function −
A string describing the template of the route.
A URL to redirect to.
An array matching the named elements in the route to regular expressions which that element should match.
Example
Make Changes in the config/routes.php file as shown below. Here, we have used controllers that were created previously.
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('/generate2', ['controller' => 'Tests', 'action' => 'index']);
$routes->redirect('/generate1','http://tutorialspoint.com/');
$routes->connect('/generate_url',['controller'=>'Generates','action'=>'index']);
$routes->fallbacks('DashedRoute');
});
Plugin::routes();
Execute the above example by visiting the following URLs.
URL 1 http://localhost:85/CakePHP/generate_url
URL 2 http://localhost:85/CakePHP/generate1
URL 3 http://localhost:85/CakePHP/generate2
Output for URL 1
Output for URL 2
You will be redirected to http://tutorialspoint.com
Output for URL 3