2

ZF2MVCアーキテクチャで使用する汎用モジュール/コントローラー/アクションルートをZendFramework2で作成したいと思います。

ZF1では、デフォルトルートは/[:module][/:controller][/:action]、モジュールがデフォルトでdefault、コントローラーがデフォルトでindex、アクションがであるように定義されていましたindex

現在、ZF2は、コントローラーとビューの単純なグループから、コントローラー名をコントローラークラスに明示的にマッピングする実際のスタンドアロンアプリケーションに、モジュールの意図する方法を変更しました。

すべてのコントローラー名はすべてのモジュールで一意である必要があるため、次のように名前を付けることを考えていましたが、上記のZF1の古いデフォルトルートのようなものを使用して、モジュールごとに特定のルートを作成する必要がないmodulename-controllernameURLのようにしたいと思います。/modulename/controllername

4

3 に答える 3

8

はい、非常に可能ですが、少し作業を行う必要があります。次の構成を使用します。

        'default' => array(
            'type'    => 'My\Route\Matcher',
            'options' => array(
                'route'    => '/[:module][/:controller[/:action]]',
                'constraints' => array(
                    'module' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    'module'     => 'default',
                    'controller' => 'index',
                    'action'     => 'index',
                ),
            ),
        ),

次に、独自に記述My\Route\Matcherして、MVC が使用できる Routemap オブジェクトを作成する必要があります。難しいことではありません。既にフレームワークに含まれている他のルート マッチャーを見てください。

于 2012-06-08T21:08:45.723 に答える
1

Zend Skeletonアプリケーションを使用する場合は、このデフォルトのコントローラーをすでに構成しています。

こちらをご覧くださいhttps://github.com/zendframework/ZendSkeletonApplication/blob/master/module/Application/config/module.config.php

于 2012-06-08T11:55:28.503 に答える
0

zf2 モジュールの汎用/標準ルーティング システムを使用するには、これが 1 つのコントローラー "module\controller\index" (デフォルト コントローラー) に対する私のソリューションです。

'router' => array(
    'routes' => array(              
        'default' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/', // <======== this is take the first step to our module "profil"
                'defaults' => array(
                    'module'     => 'profil',
                    'controller' => 'profil\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),              
        'profil' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/[profil][/:action]', // <======== this is take the next steps of the module "profil"
                'constraints' => array(
                    'module' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array( // force the default one
                    'module'     => 'profil',
                    'controller' => 'profil\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

次に、コントローラー「profil\Controller\Index」には、「index」「home」「signout」という 3 つのアクションがあります。

public function indexAction()
{
        if ($this->identity()) {
            return $this->redirect()->toRoute('profil',array('action'=>'home'));
        } else {
            // ......
                    $authResult = $authService->authenticate();
                    if ($authResult->isValid()) {
                            //......
                                                    return $this->redirect()->toRoute('profil',array('action'=>'home'));
                    } else {
                        // ......
                    }
                } else {
                    $messages = $form->getMessages();
                }
            }               
            return new ViewModel();
        }
}

public function homeAction()
{
    if (!$this->identity()) {
        return $this->redirect()->toRoute('profil',array('action'=>'signout'));
    }
}

public function signoutAction()
{
    if ($this->identity()) {
        $authService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');
        $authService->clearIdentity();
    }
    $this->redirect()->toRoute('profil');
}  

とにかくありがとう:)

于 2014-02-26T10:42:34.323 に答える