views:

93

answers:

5

Hi folks,

setting up my authentication, in my users_controller i have a register action listed below. I'm getting an error at line 20 which is

if (!empty($this->data)) { 

The error is:

syntax error, unexpected T_STRING

Here is my entire users_controller:

<?php
class UsersController extends AppController {
var $name = 'Users';
var $helpers = array('Time', 'Crumb', 'Html', 'Form');
var $components = array('Auth');


function index() {
    $this->set('users', $this->User->find('all'));
}

function view($id = null) {
    $this->User->id = $id;
    $this->set('user', $this->User->read());
}

function register() {
if (!empty($this->data)) {
if ($this->data['User']['password'] == $this->Auth->password($this->data['User']['password_confirm'])) {
$this->User->create();
$this->User->save($this->data);
$this->redirect(array('action' => 'index'));
}
}
}   

function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index');
}

function login() {
}

function logout() {
    $this->redirect($this->Auth->logout());
}


}



?>

Can anyone see whats wrong?

Jonesy

+2  A: 

You're missing a couple hyphens on line 19.

if ($this->data['User']['password'] == $this->Auth >password($this>data['User']['password_confirm'])) {

should be

if ($this->data['User']['password'] == $this->Auth->password($this->data['User']['password_confirm'])) {

I think you're more than capable of checking your own syntax errors in the future.

erjiang
thats an error in my posting, it's fine in my code. still getting the error :( can u retract the -1
iamjonesy
@Jonesy You have a syntax error in your code somewhere. It is pointless to post code that is not 100% what your code looks like.
deceze
A: 

Reason(s):

The empty function will not work on an attribute of an object when the object uses the __get method (and maybe AppController class is doing so).

Also, remember (not for your case, though): empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).

Also, (not for your case, though): because empty is a language construct and not a function, it cannot be called using variable functions

Solution:

Therefore, please create your own empty function or directly compare values.

shamittomar
this is a strange one, i've deleted the if empty statment, line 20 is now white space but i'm still getting the same error message coming from line 20
iamjonesy
Can you paste the new code with line numbers (maybe in pastebin.com or something similar) ?
shamittomar
shamittomar, if you use the bake code generation script, empty is used in similar fashion. hence there is no need for the OP to create his/her own `empty` method.
benjamin
A: 

Hello Jonesy, next time please highlight the line number under concern somehow. If you are on linux cat -n for prefixes every line with its number. Reading through the other answers, it is a miracle to me how hyphens could vanish during your copy-and-paste to SO. Therefore I suggest you check if the Auth component is really in your components variable. Is the name of the form field really password_confirm?

Kind regards, Benjamin.

benjamin
A: 

Please post the error text. It's entirely possible that it refers to line 20 in the view (ctp file), rather than the controller, and seems likely as there is nothing obvious wrong with your code. A mistake I'm guilty of making myself now and again.

Leo
A: 

If you are using Auth there are a few things you may need to consider. In the beforeFilter you need to add:

$this->Auth->allow('register', 'login', 'logout');

This will give anyone permission to access these.

What you need to know is how to trouble shoot your syntax errors. Here is what I suggest to start with. Right before line 20 put:

pr($this->data);
exit;

Run it again and see what is output to the browser. You will be able to see what values are coming in (if any) and it may lead to a solution.

cdburgess