0

私は次のようなコントローラを持っています:

public function index() {

        $data['content'] = 'homepage/login';
        $this -> load -> view('templates/no_jsTemplate', $data);
    } 

index() はログインページです (最初に表示されるページ)

インデックス ページには、投稿データを に送信するフォームがあります。

public function login_user() {
        $this -> load -> library('form_validation');

        if ($this -> form_validation -> run('c_homepage/login_user') == FALSE) {
            $this -> index();
        } else {
            $this -> load -> model('m_homepage');
            $this -> m_homepapge -> login_user();
        }

    } 

これを凝縮することは可能ですか?1 ページに 2 つのコントローラー関数があるのは過剰だと思いませんか?

私はこれを試しました:

public function index() {
        if (!$this -> input -> post('login_user')) { // check if submit is clicked, if not then just load the data array and show the view
            $data['content'] = 'homepage/login'; 
            $this -> load -> view('templates/no_jsTemplate', $data);
        } else { // if it is clicked load the library and then do the validation and then load the model if validation passes and then do the login_user business calculation 
            $this -> load -> library('form_validation');

            if ($this -> form_validation -> run('c_homepage/login_user') == FALSE) {
                $this -> index();
            } else {
                $this -> load -> model('m_homepage');
                $this -> m_homepapge -> login_user();
            }
        }
    } 

しかし、これを行うと、ある種の無限ループに陥ります。ビューをロードする関数と、コントローラーがデータをモデルに送信する関数を持つ他のアプリケーションを作成しました。物事を行うための他のより良い方法を学びたいだけです。私の成績はこのプロジェクトに反映されます。私の主な目標は、できるだけDRYになることです. 私はより多くのバンのために少ないコードを書きたいと思っています。うまくいけば、これはあまり求めていません。

4

3 に答える 3

0

検証ルールをconfig/form_validation.phpファイルに移動する必要があります。サイトで多数のフォームを使用する必要がある場合は、フォーム検証ライブラリを自動ロードするのが最適な場合があります。

class Controller extends CI_Controller { パブリック関数 index();

public function method()
{

      //If you set the config file to match
      //the controller and method name,
      //you won't need to add it as a param.
      if(!$this->form_validation->run())
      {
          return $this->index();
          //or if your using ajax, you would just send back a status
          //and handle errors on frontend
          return $this->output->set_status_header(401);//un-authorized
      }

      //we got this far so validation must have passed :)
}

}

-

$config = array(
    'controller/method'  =>  array(
        array('field'=>'name', 'label'=>'Name', 'rules'=>'required|xss_clean')),
);
于 2013-08-13T18:34:26.930 に答える
0

次の行では、ロジックを示します。コードをDRYに保つことを検討することをお勧めします。

これらの行をコピーして貼り付けることは避けてください。プロジェクトで機能するように操作する必要があります。中のコメントもよく読んでください。

public function index()
{
    if (isset($_POST['login_user'])) {
        // Load validation library
        $this -> load -> library('form_validation');

        // Set your validation rules by using $config array
        $this->form_validation->set_rules($config);

        if ($this->form_validation->run() == FALSE) {
            // Store the validation error messages
            $data['error'] = validation_errors();
        } else {
            // Do the login process
            $this->load->model('m_homepage');
            $this->m_homepapge->login_user();

            // After user is logged in, do you need to say Welcome to him/her?
            // You might want to fetch user's name from database, so:
            // $data['success'] = "Hello $name, You're logged in." 
            // 
            // If you need to show the `Successfully` message to user at once
            // Store the message in flashdata:
            // $this->session->set_flashdata('success', 'message');
            // 
            // Do you want to redirect user to somewhere else?
            // redirect('route', 'refresh');
        }
    } else {
        // This method is called directly and/or without any login POST data.
        // Do you need to do somethin special in this case? put your logic here.
    }

    // Always send a view to the user,
    // If user doesn't send the POST data, it shows the login form,
    // Or if user made a mistake during filling the login form,
    //      you can show the `error` to him/her and also show the form inputs.
    // Or if the user is logged in successfully,
    //      You can show the `success` message to him/her,
    //      Or redirect him/her to another route as mentioned before.
    $data['content'] = 'homepage/login'; 
    $this->load->view('templates/no_jsTemplate', $data);
}

ノート:

検証ルールを構成ファイルに保存した場合は、メソッドを使用してルールを設定する必要application/config/form_validation.phpはありませ(私が行ったように)ただし、が変更されているため、名前を自分のものに変更する必要があります。この場合、次のようになります。set_rules()Controller/MethodRule Groupc_homepage/index

$config = array(
    'c_homepage/index' => array(
        array(
                'field' => 'username',
                'label' => 'Username',
                'rules' => 'required'
        ),
        array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required'
        ),
        array(
                'field' => 'passconf',
                'label' => 'PasswordConfirmation',
                'rules' => 'required'
        ),
        array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required'
        )
    )
);

ルール グループがコントローラ クラス/関数と同じ名前である場合、run() 関数がそのクラス/関数から呼び出されると、自動的に使用されます。

- CI ユーザー ガイド

それが役に立てば幸い。

于 2013-08-13T14:46:01.710 に答える