在注册操作中出现烦人的错误。意外的 T_STRING

发布于 2024-09-15 09:32:05 字数 1222 浏览 3 评论 0原文

设置我的身份验证,在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枫以 2024-09-22 09:32:05

第 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.

只为守护你 2024-09-22 09:32:05

请发布错误文本。它完全有可能引用视图(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.

倾城°AllureLove 2024-09-22 09:32:05

如果您使用 Auth,则可能需要考虑一些事项。在 beforeFilter 中,您需要添加:

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

这将授予任何人访问这些内容的权限。

您需要知道的是如何解决语法错误。这是我建议开始的内容。在第 20 行之前放置:

pr($this->data);
exit;

再次运行它并查看浏览器输出的内容。您将能够看到传入的值(如果有),这可能会带来解决方案。

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.

情话墙 2024-09-22 09:32:05

原因:

当对象使用 __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 functions

Solution:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文