Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outputting a hyperlink from a controller in cakePHP

I'm just getting started with cakePHP, and things aren't going so well so far.

I have a controller that handles confirming user emails. On registration the user is sent an email with a confirmcode in a link. Depending on the confirm code they give, the controller gives different text responses. One of these responses includes a hyperlink in order to log in.

I'm trying to use the Html helper, but although I've loaded it in $helpers at the top of the class, I an only make it work if I then use App::import, and then instantiate it.

It all seems overkill to simply make a hyperlink! How many times do I have to load the same class?

Wherever I look on the web it keeps telling me it's a bad idea to use a helper in a controller, but how else am I supposed to get the link made?

So I have

var $helpers = array('Html');

at the top of the controller, and:

if (isset($this->User->id)) { // Check the user's entered it right
  // Do some stuff to remember the user has confirmed
  // This is to load the html helper - supposedly bad form, but how else do I make the link?
  App::import('Helper', 'Html');
  $html = new HtmlHelper();
  $this->set('message', __("Your email address has been confirmed.", TRUE)." ".$html->link(__("Please log in", TRUE), array('controller' => "users", 'action' => "login" )));
} else {
  $this->set('message', __("Please check your mail for the correct URL to confirm your account", TRUE));
}

in the controller's confirm method and

<div>
   <?php echo $message;?>
</div>

in the view to output the resulting message

Surely I'm going wrong somewhere - can anyone explain how?

like image 673
Hippyjim Avatar asked Sep 28 '10 00:09

Hippyjim


2 Answers

You're not supposed to use Helpers in the Controller. As @Lincoln pointed out, you should construct the link in the View. You may construct the URL in the Controller, since a URL is basically data, but a link is a very medium-specific (HTML) implementation of a URL.

Either way, you'll need to create a full URL (including host) if you want to send it in an Email. The most universal way is to use Router::url:

$fullUrl = Router::url(array('controller' => ...), true); // 'true' for full URL

Do this in either the Controller or the View. For creating a link, use this in the View:

echo $html->link('Title', $fullUrl);
like image 98
deceze Avatar answered Nov 02 '22 19:11

deceze


The idea is that all the data you need to render the page is sent to the view with set, then any conditional logic or formatting is done in the view with helpers, so send whole query results when appropriate (suppose you need to alter a link to include the user's screen name, you'll have it handy).

in controller action

$this->set('user', $this->User);

in view (this is slightly different depending on if your in <= 1.2 or 1.3

if ($user->id) //available because of Controller->set
{
    //1.2
    $link = $html->link(__("Please log in", TRUE), array('controller' => "users", 'action' => "login" ));
    //1.3
    $link = $this->Html->link(__("Please log in", TRUE), array('controller' => "users", 'action' => "login" ));
    echo __("Your email address has been confirmed.", TRUE)." $link";
}
else
{
    $this->set('message', __("Please check your mail for the correct URL to confirm your account", TRUE));
}

like image 23
Lincoln B Avatar answered Nov 02 '22 19:11

Lincoln B