Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP Redirect to External URL

Tags:

cakephp

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.

like image 500
user1074139 Avatar asked Nov 30 '11 19:11

user1074139


3 Answers

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.

like image 194
Vins Avatar answered Nov 09 '22 23:11

Vins


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' );
like image 25
Farray Avatar answered Nov 09 '22 23:11

Farray


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.

like image 32
jwg2s Avatar answered Nov 10 '22 01:11

jwg2s