我创建了一个Register_Controller
并为其设置了route
,如下所示。 RegisterForm呈现正常,但是当您点击“注册”时,它会转到以下网址:http://website.com/Register_Controller/RegisterForm
并显示“找不到网页”,因此未点击doRegister
方法。
我做错了什么?
Routes.yml
---
Name: mysiteroutes
After: framework/routes#coreroutes
---
Director:
rules:
'register': 'Register_Controller'
Register_Controller.php
<?php
class Register_Controller extends Page_Controller {
private static $allowed_actions = array(
'RegisterForm'
);
public function index() {
return $this->renderWith(array('RegisterPage', 'Page'));
}
public function RegisterForm(){
return new RegisterForm($this, 'RegisterForm');
}
}
RegisterForm.php
<?php
class RegisterForm extends Form {
public function __construct($controller, $name) {
$fields = new FieldList(
TextField::create('FirstName'),
TextField::create('Surname'),
TextField::create('Email'),
PasswordField::create('Password'),
PasswordField::create('ConfirmPassword'),
TextField::create('Username')
);
$actions = new FieldList(
new FormAction('doRegister', 'Register')
);
$validator = new RequiredFields(
'Email', 'Password', 'ConfirmPassword', 'Username'
);
parent::__construct($controller, $name, $fields, $actions, $validator);
$this->disableSecurityToken();
if (Session::get('RegisterFormData')) {
$this->loadDataFrom(Session::get('RegisterFormData'));
}
}
public function doRegister($data, $form) {
$checkIfEmailExsists = Member::get()->filter('Email', $data['Email'])->first();
$registerFormData = Session::set('RegisterFormData', $data);
if ($checkIfEmailExsists) {
$form->addErrorMessage('Email', 'This email already exists', 'bad');
return $this->controller->redirectBack();
}
$member = new Member();
$form->saveInto($member);
$password = $data['Password'];
$member->changePassword($password);
$member->write();
$member->addToGroupByCode('administrators', 'Administrators');
$member->logIn();
Session::clear('RegisterFormData');
return $this->controller->redirect($this->controller->Link('thanks'));
}
}
RegisterPage.ss
<h2>Register Here</h2>
$RegisterForm
答案 0 :(得分:1)
您的表单正在尝试从控制器获取基本链接,但由于它无法在自定义路由控制器上找到,因此它使用控制器名称。您需要添加Link()方法。
class Register_Controller extends Page_Controller {
public function Link($action = null) {
return Controller::join_links(Director::baseURL(), '/register/', $action);
}
}