2016-03-26 8 views
0

私はRegister_Controllerを作成し、以下のようにrouteをセットアップしました。 RegisterFormはうまく描画されますが、[登録]をクリックすると、次のURLに移動します: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 

答えて

1

フォームは、コントローラからベースリンクを取得しようとしているが、それはカスタムルートコントローラ上のものを見つけることができないので、それはコントローラ名を使用しています。 Link()メソッドを追加する必要があります。

class Register_Controller extends Page_Controller { 
    public function Link($action = null) { 
     return Controller::join_links(Director::baseURL(), '/register/', $action); 
    } 
} 
+0

ありがとうございました!これも機能します: 'public function Link($ action = null){ return Controller :: join_links( 'register'、$ action); } ' – ifusion

関連する問題