user1074139
user1074139

Reputation: 71

CakePHP Redirect to External URL

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.

Upvotes: 7

Views: 33330

Answers (4)

DoctorFox
DoctorFox

Reputation: 173

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

Upvotes: 0

jwg2s
jwg2s

Reputation: 816

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.

Upvotes: 2

Vinayak Phal
Vinayak Phal

Reputation: 8919

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.

Upvotes: 16

Farray
Farray

Reputation: 8528

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' );

Upvotes: 6

Related Questions