3

コハナ3.1の認証モジュールに取り組んでいます。私のモジュールのinit.phpで..。

Route::set(

    'a11n',
    '<controller>',
    array(
        'controller' => 'signIn|signOut|signUp'
    )

);

Kohanaのルーティングメカニズムを100%使用する方法はわかりませんが、これを使用して、ユーザーが「signIn」、「signOut」、または「signUp」と入力してモジュールからコントローラーを実行できるようにしようとしています。ほら、私は「ポータブル」認証システムが欲しいのです...だから私は単に正しいディレクトリを「コピー&ペースト」し、モジュールを有効にして、私のサイトに認証を持たせることができます。

このルートでは、デフォルトルートの動作を変更したくないことを覚えておいてください。私のコードがどれほど正しいかわかりません...しかし、それは機能します!テストしたところ、3番目のパラメーターを使用しなくても同じ効果を得ることができます。私は今それで何を達成しますか?

そして今、質問...「サインイン」と入力してモジュール「Controller_SignIn」を実行するモジュールからルートを設定するにはどうすればよいですか?

4

3 に答える 3

4

これを行うには、次のようなルートを使用する必要があります。

Route::set('SignIn', '/sign-in(/<action>)',
        array(
            'action' => 'index|action1',
            )
        )
        ->defaults(
                array(
                    'controller' => 'SignIn',
                    'action' => 'index',
                    )
                );

Route::set('SignOut', '/sign-out(/<action>)',
        array(
            'action' => 'index|action1',
            )
        )->defaults(
                array(
                    'controller' => 'SignOut',
                    'action' => 'index',
                    )
                );

また

Route::set('SignIn', '/sign-in/',
        array()
        )
        ->defaults(
                array(
                    'controller' => 'user',
                    'action' => 'login',
                    )
                );

Route::set('SignOut', '/sign-out/)',
        array()
        )->defaults(
                array(
                    'controller' => 'user',
                    'action' => 'logout',
                    )
                );
于 2011-06-04T21:07:05.413 に答える
2

私はこの質問にすでに解決策としてマークされた答えがあることを知っていますが、それを行うためのよりクリーンな/別の方法があります:

アプリケーションに新しいファイルを作成しapplication/classes/request.php、そのファイルに次のコードを追加します。

<?php defined('SYSPATH') or die('No direct script access.');
class Request extends Kohana_Request
{
    public function execute()
    {
        $this->action(str_replace('-', '', $this->action()));
        $this->controller(str_replace('-', '', $this->controller()));
        return parent::execute();
    }
}

これで、破線/ハイフンでつながれたURLごとにbootstrap.phpを変更/汚染する必要がなくなりました。

于 2011-10-15T09:55:07.983 に答える
1

アカウントアクション用に個別のコントローラーを作成するのはなぜですか?必要なアクションを使用して1つのコントローラー(Controller_Accountまたは他の何か)を作成します。

class Controller_Account extends Controller_Template {

    public function action_signin() {...}

    public function action_signout() {...}

    public function action_signup() {...}

}

ご覧のとおり、アクション名にはダッシュがありません。メソッド名には使用できません。しかし、ここにそれのハックがあります:

public function before()
{
    parent::before(); // dont forget this call!
    // remove dashes from current method name
    $this->request->action(str_replace('-', '', $this->request->action()));
}

そしてルート:

Route::set(
       'a11n', 
       '<action>', 
       array('action' => array('sign-in|sign-up|sign-out'))
    )
    ->defaults(array('controller' => 'account'));

もちろん、サインイン名とサインイン名の両方を使用できます。破線以外の名前をRouteregexparamに追加するだけです。

Route::set(
       'a11n', 
       '<action>', 
       array('action' => array('sign-in|sign-up|sign-out|signin|signup|signout'))
    )
    ->defaults(array('controller' => 'account'));
于 2011-06-04T19:40:17.430 に答える