CakePHP redirect to external URL

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

Example: example.com/google redirects to http://www.google.com

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

+4
source share
4 answers

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

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

from our controller. It will redirect you to the specified address. It works great.

+11
source

You do not want to redirect, you want to create a hyperlink.

Use the built-in Html Cake helper.

In your controller ...

 var $helpers = array( 'Html' ); 

In your opinion ...

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

Redirects are typically used to refer to a server-side redirect script. For example, after a user fills out a contact form, you can send us your details by e-mail, and then redirect the user to "Success!". pages with the following controller code

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

Using the CakePHP HTML Helper is the best choice.

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

If it's simple enough, you can just use direct HTML.

+1
source

You need something like:

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

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

0
source

Source: https://habr.com/ru/post/1383975/


All Articles