views:

40

answers:

3

Hi

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?

+1  A: 

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

Lincoln B
Aaah, ok! That makes a bit more sense. It was the idea of passing the full user data to the view that I didn't get. I saw the view as far more "dumb". Thanks for the good clear illustration.
Hippyjim
how can you tell someone to pass the User model to the view? that is breaking MVC in the biggest way ive seen.
dogmatic69
I thought $this->User was as query result rather than a model. Your right, you should be doing $user = $this->User->find() then $this->set('user', $user) , so user would be a result array in the view.
Lincoln B
Yeah, I understood it to be the data, rather than the model - passing the model could risk all sorts of nasty circular references happening, right? (not to mention breaking the MVC)
Hippyjim
+2  A: 

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);
deceze
A: 

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

dogmatic69
Thanks for the pointers - but in this case the flash isn't enough, as there's more than just a single "div" to populate - I just kinda cut the code down for the example. Either way, letting the view handle the display logic makes much more sense to me, and I've gotten the code working.
Hippyjim

related questions