1

Zend Framework 2 ルーターについて質問があります。次のようなmysqlにテーブル「seourl」があります。

url             module          controller         action          param
test123         catalog         product            view            5       
abc123          catalog         product            view            6
other           catalog         category           view            10

これらの URL をルーターに含めたい。

URLフィールドには、次のようなURLを含めることができます:others/product/(このテーブルから任意のタイプのURLをルーティングしたい)

前もって感謝します。

後で編集:

このテーブルから各 URL をルーティングしたいと考えています。

例:

example.com/test123 は module catalog/ controller product/ action view/ paramをロードします5

example.com/other は module catalog/ controller category/ action view/ paramをロードします10

4

1 に答える 1

4

これを行う 1 つの方法は、アプリケーションの「ルート」イベントにイベント (優先度 > 0、これは重要です!) をアタッチすることです。この正の優先度により、ルート マッチングが発生する前にハンドラーが実行されます。つまり、独自のルートを追加する機会があります。

次のようなもの。これはどこでもテストされていないことに注意してください。そのため、いくつかのものをクリーンアップする必要がある場合があります。

<?php
namespace MyApplication;

use \Zend\Mvc\MvcEvent;
use \Zend\Mvc\Router\Http\Literal;

class Module {

    public function onBootstrap(MvcEvent $e){
        // get the event manager.
        $em = $e->getApplication()->getEventManager();

        $em->attach(            
            // the event to attach to 
            MvcEvent::EVENT_ROUTE,           

            // any callable works here.
            array($this, 'makeSeoRoutes'),   

            // The priority.  Must be a positive integer to make
            // sure that the handler is triggered *before* the application
            // tries to match a route.
            100
        );

    }

    public function makeSeoRoutes(MvcEvent $e){

        // get the router
        $router = $e->getRouter();

        // pull your routing data from your database,
            // implementation is left up to you.  I highly
            // recommend you cache the route data, naturally.               
        $routeData = $this->getRouteDataFromDatabase();

        foreach($routeData as $rd){
                        // create each route.
            $route = Literal::factory(array(
                'route' => $rd['route'],
                'defaults' => array(
                    'module' => $rd['module'],
                    'controller' => $rd['controller'],
                    'action' => $rd['action']
                )
            ));

            // add it to the router
            $router->addRoute($route);
        }
    }
}

これにより、アプリケーションが routeMatch を見つけようとする前に、カスタム ルートが確実にルーターに追加されます。

于 2013-06-08T22:26:28.773 に答える