开发者

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 li开发者_如何学JAVAnk. 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?


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


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


What you are trying to do should be done with the SessionComponent. $this->Session->setFlash('your message here');

and in your layout with the session helper put $this->Session->flash();

About your wanting urls in the controller, Router::url is correct as deceze said, but there is no use for it there as you should not be building html in a controller.

what you want to do it use the session::setFlash() method above and then redirect them using

$this->redirect(array('controller' => "users", 'action' => "login" ));

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜