12

私は現在、スケルトン アプリに大まかに基づいて小さな MVC アプリケーションを開発することで ZF2 を学習しています。現在、一致したルートに基づいていくつかの固定 HTML 要素を非表示にしようとしています。例として、ログイン フェーズ中にメイン メニューを表示したくありません。

コントローラのアクションからの戻り値としてトグル パラメータを渡すことで簡単に実現できますが、それではうまくいかないので、レイアウトから一致するルートを確認し、それに応じてレイアウトを構成したいと思います。

問題は、テンプレートで一致したルートを取得する方法がわからないことです。これは可能ですか?コントローラにレイアウト ロジックを追加しないようにする他の解決策はありますか?

いくつかの優れたフランケンシュタインの仕事の後、編集して、これに対する解決策を見つけることができました. 私はヘルパーを使用するというアイデアが好きなので、メイン モジュールのブースト機能から Application オブジェクトを渡そうとしました。

$app = $e->getApplication();
$serviceManager = $app->getServiceManager();
....
$serviceManager->get('viewhelpermanager')->setFactory('getRoute', function($sm) use ($app) {
    return new Helper\GetRoute($app);
});

およびヘルパー関数:

use Zend\View\Helper\AbstractHelper;

class GetRoute extends AbstractHelper {
    private $sm;

    public function __construct($app) {
        $this->sm = $app->getServiceManager();
    }

    public function echoRoute() {
        $router = $this->sm->get('router');
        $request = $this->sm->get('request');

        $routeMatch = $router->match($request);
        if (!is_null($routeMatch))
            echo $routeMatch->getMatchedRouteName();
    }
}

おそらく、これを行うためのよりクリーンでZF2っぽい方法があります...

4

10 に答える 10

31

新しい一致のない別のソリューション

$routeMatch = $serviceLocator->get('Application')->getMvcEvent()->getRouteMatch();

echo $routeMatch->getMatchedRouteName();
于 2013-01-11T09:59:19.543 に答える
12

レイアウトでサービスマネージャーを取得する方法があります:

$sm = $this->getHelperPluginManager()->getServiceLocator();

$sm->get('router')などにアクセスできます。

于 2012-11-16T20:06:11.797 に答える
9

You could create a View helper that implements ServiceManagerAwareInterface. Then inside the View helper using the ServiceManager instance to retrieve both the router and request objects then reconstruct the route match.

$services = $this->getServiceManager();

$router = $services->get('router');
$request = $services->get('request');

$routeMatch = $router->match($request);
echo $routeMatch->getMatchedRouteName();

I'd also recommend writing the View helper so that code only triggers once per request.

于 2012-08-24T09:27:04.243 に答える
1

アクション/コントローラー名を見つけることで解決できると思います:

$controller = $this->getRequest()->getControllerName();
$action = $this->getRequest()->getActionName();

アクションがわかれば、レイアウトのセクションを有効にする特定の条件を設定できます。

于 2012-08-22T09:08:46.727 に答える
1

私はあなたが使用できると思います

$this->getHelperPluginManager()->getServiceLocator()->get('Application')->getMvcEvent()->getRouteMatch()->getMatchedRouteName();
于 2016-06-11T18:10:00.747 に答える
1

getRouteMatch を ZF3 に統合するための「Rodrigo Boratto」投稿に関する追加情報 (50 未満のレポがあるため、コメントできません...)

ビュー ヘルパー ファイルでは、次の行が表示されます。

use Zend\Mvc\Router\RouteMatch as MvcRouteMatch;
use Zend\Mvc\Router\RouteStackInterface;

次のようにする必要があります。

use Zend\Router\RouteMatch as MvcRouteMatch;
use Zend\Router\RouteStackInterface;

彼らがいつその変更を行ったのかはわかりませんが、ファイルは Zend\Router 名前空間にあります。

PSそれが重要な場合は、作曲家を使用します。

于 2016-11-09T14:01:18.817 に答える
0

私のコントローラー:

    <?PHP
    namespace SomeName\Controller;

    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;

    class SomeController extends AbstractActionController
    {
        public function getIdAction()
        {
            $id = $this->params()->fromRoute('id', 0);
            return new ViewModel(array(
                'id' => $id,
            ));
        }
    }

私のルーター:

    <?php
    return array(
        'controllers' => array(
            'invokables' => array(
                'SomeName\Controller\Some' => 'SomeName\Controller\SomeController',
            ),
        ),

        'router' => array(
            'routes' => array(
                'testId' => array(
                    'type'    => 'segment',
                    'options' => array(
                        'route'    => '/[:id]',
                        'constraints' => array(
                            'id' => '\d*',
                        ),
                        'defaults' => array(
                            'controller' => 'SomeName\Controller\Some',
                            'action'     => 'getId',
                        ),
                    ),
                ),
            ),
        ),

        'view_manager' => array(
            'template_path_stack' => array(
                'album' => __DIR__ . '/../view',
            ),
        ),
    );
于 2013-01-30T21:33:23.850 に答える