Will
Will

Reputation: 21

Helper not available in the view

I am migrating from 1.3 to 2.0.6 and have the following problem:

Notice (8): Undefined variable: session [APP/View/Users/login.ctp, line 2]

Fatal error: Call to a member function flash() on a non-object in login.ctp on line 2

Why is there a problem accessing the session helper?

Upvotes: 1

Views: 76

Answers (1)

AD7six
AD7six

Reputation: 66208

That's not how to use helpers

Notice (8): Undefined variable: session [APP/View/Users/login.ctp, line 2]

Fatal error: Call to a member function flash() on a non-object in login.ctp

Whilst it's not in the question your login.ctp template looks something like this:

<?php
echo $session>flash(); # line 2

That's not how helpers are expected to be used in CakePHP 2.x:

you can use [the helper] in your views by accessing an object named after the helper:

<!-- make a link using the new helper -->
<?php echo $this->Link->makeEdit('Change this Recipe', '/recipes/edit/5'); ?>

The code you're looking for is:

# echo $session->flash(); # wrong
echo $this->Session->flash(); # right

Note also that Session needs to be in the controller $helpers array.

In 1.2 and earlier versions of CakePHP helpers were expected to be variables, this changed but was still supported in 1.3. If your 1.3 CakePHP application has been using helpers in this way, it has been relying on backwards-compatibility with earlier versions. In 2.0 helpers are not variables, and only possible to access as a view class property. Be sure to read the migration guides - for more information on what has changed.

Upvotes: 2

Related Questions