0

同じディレクトリAPP/controllerに2つのコントローラーaccount.phpとaddress.phpがあります。最初のコントローラー account.php では、次のような URI にデフォルト ルーターを使用します: account/create、account/login、account/logout... /。ここでわかるように、アカウント コントローラーと一致する同じ URI ルーターを使用しています。

私の最初のアプローチ:

// Add new address
Route::set('address', 'account/address/<action>')
        ->defaults(array(
                'controller' => 'address',
                'action'     => 'index',
        ));

私のアドレスコントローラー

public function before()
{
    parent::before();

    if ( ! Auth::instance()->logged_in())
    {
        // Redirect to a login page (or somewhere else).
        $this->request->redirect('');
    }
}



// nothing here
public function action_index()
{
    $this->request->redirect('');
}


// create account
public function action_create()
{

    // Create an instance of a model
    $profile = new Model_Address();

    // received the POST
    if (isset($_POST) && Valid::not_empty($_POST)) 
    {   

        // // validate the form
        $post = Validation::factory($_POST)
        ->rule('firstname', 'not_empty')
        ->rule('lastname', 'not_empty')
        ->rule('address', 'not_empty')
        ->rule('phone', 'not_empty')
        ->rule('city', 'not_empty')
        ->rule('state', 'not_empty')
        ->rule('zip', 'not_empty')
        ->rule('country', 'not_empty')
        ->rule('primary_email_address', 'email');

        // if the form is valid and the username and password matches
        if ($post->check()) 
        {

            if($profile->create_profile($_POST)) 
            {
                $sent = kohana::message('profile', 'profile_created');
            }

        } else {
            // validation failed, collect the errors
            $errors = $post->errors('address');
        }

    }

    // display
    $this->template->title = 'Create new address';
    $this->template->content = View::factory('address/create')
            ->bind('post', $post)
            ->bind('errors', $errors)
            ->bind('sent', $sent);
}

問題ないようですが、ルーターのアカウント/アドレス/作成が機能していません。Kohana は、その URI に対して 404 メッセージをスローします。

なぜそれが起こるのか誰にも分かりますか?

4

1 に答える 1

4

システムでルートを確認したところ、問題なく動作しているため、エラーは別の場所にあると思われます。ドキュメントが言うように、コントローラーのクラス名がController_Addressであるかどうかを確認し、このルートをブートストラップの最初のルートとして配置してみてください。

ルートは追加された順序で照合されることを理解することが重要です。URL がルートと一致するとすぐに、ルーティングは基本的に「停止」され、残りのルートは試行されません。デフォルト ルートは、空の URL を含むほぼすべてのものと一致するため、新しいルートはその前に配置する必要があります。

質問に関係のない別のこと-モデル内に検証を配置する必要があります。コントローラーはその場所ではありません;)

于 2012-04-04T06:38:44.393 に答える