在注册操作中出现烦人的错误。意外的 T_STRING
设置我的身份验证,在我的 users_controller 中,我有一个注册操作,如下所示。我在第 20 行收到错误,
if (!empty($this->data)) {
错误是:
syntax error, unexpected T_STRING
这是我的整个 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());
}
}
?>
任何人都可以看到出了什么问题吗?
琼西
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第 19 行缺少几个连字符。
if ($this->data['User']['password'] == $this->Auth >password($this>data[' User']['password_confirm'])) {
应该是
if ($this->data['User']['password'] == $this->Auth->password ($this->data['User']['password_confirm'])) {
我认为您将来有能力检查自己的语法错误。
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.
请发布错误文本。它完全有可能引用视图(ctp 文件)中的第 20 行,而不是控制器,并且看起来很可能是因为您的代码没有任何明显的错误。这是我时常犯下的错误。
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.
如果您使用 Auth,则可能需要考虑一些事项。在
beforeFilter
中,您需要添加:这将授予任何人访问这些内容的权限。
您需要知道的是如何解决语法错误。这是我建议开始的内容。在第 20 行之前放置:
再次运行它并查看浏览器输出的内容。您将能够看到传入的值(如果有),这可能会带来解决方案。
If you are using Auth there are a few things you may need to consider. In the
beforeFilter
you need to add: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:
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.
原因:
当对象使用 __get 方法时,
empty
函数将无法作用于对象的属性(也许 AppController 类正在这样做)。另外,请记住(但不适用于您的情况):
empty()
仅检查变量,因为其他任何内容都会导致解析错误。换句话说,以下内容将不起作用:empty(trim($name))
。另外,(但不适合您的情况):因为
empty
是一种语言构造而不是函数,因此不能使用变量函数调用它解决方案:
因此,请创建您自己的空函数或直接比较值。
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 functionsSolution:
Therefore, please create your own empty function or directly compare values.