6
votes

In CakePHP, I want to create a custom URL that points from my site to another site.

Example: example.com/google would redirect to http://www.google.com

I'm a self-taught CakePHP newcomer and just can't figure out the steps. From my homework, I think I can create a route to a controller/action in config/routes.php, but I don't the right terminology to create the action in the controller.

4

4 Answers

14
votes

If you want to redirect directly form controller to an external url the we can directly use

$this->redirect('http://www.google.com');

from our controller. It will redirect you to the mentioned address. This works perfectly fine.

5
votes

You don't want a "redirect", you want to create a hyperlink.

Use Cake's built-in Html helper.

In your controller...

var $helpers = array( 'Html' );

In your view...

echo $this->Html->link( 'Google link!', 'http://www.google.com/' );

A "redirect" is commonly used to refer to redirecting the script on the server side. For example, after a user fills out a Contact form you may want to email yourself the details and then redirect the user to a "Success!" page with the following controller code

$this->redirect( '/contact/success' );
2
votes

Using CakePHP HTML helper is your best bet.

echo $this->Html->link('Link Text Here', 'http://www.anywebsiteyouwant.com);

If it's simple enough, you could just use straight HTML.

0
votes

What you need is something like:

Router::redirect('/posts/*', 'http://google.com', array('status' => 302));

This would redirect /posts/* to http://google.com with a HTTP status of 302. See http://book.cakephp.org/2.0/en/development/routing.html