0

私は Zend を初めて使用したばかりで、Zend ルーターに少し問題があります。調べてみましたが、何も見つかりませんでした...

uri レベルで定義された変数ごとにルーターを定義して、1 つのコントローラーで別のアクションを指すことができるようにしたいと考えています。

私は言語とモジュールを扱っているので、ブートストラップ アプリケーションで次の initRoutes 関数を定義しました。

protected function _initRoutes()
{
    $front = Zend_Controller_Front::getInstance();
    $router = $front->getRouter();

    $defaultRoute = new Zend_Controller_Router_Route(
        ':lang/:module/:controller/:action',
        array(
            'lang' => 'es',
            'module' => 'default',
            'controller' => 'index',
            'action' => 'index'
        ),
        array(
            'lang' => '^(en|es)$',
            'module' => '^(default|admin)$'
        )
    );

    $router->addRoute('defaultRoute', $defaultRoute);

    return $router;
}

定義されたアクションによって、フォーラム セクションとフォーラム トピックにアクセスできるようにしたいと考えています。

何かのようなもの :

  • マイドメイン/フォーラム -> フォーラム/インデックス

  • マイドメイン/フォーラム/セクション -> フォーラム/セクションアクション

  • マイドメイン/フォーラム/セクション/トピック -> フォーラム/トピックアクション

また、次のような uri レベルで定義された lang とモジュールを使用します。

  • 私のドメイン/言語/モジュール/フォーラム

  • 私のドメイン/言語/モジュール/フォーラム/セクション

  • 私のドメイン/言語/モジュール/フォーラム/セクション/トピック

だから私はこれを持っています:

class ForumController extends Zend_Controller_Action
{

public function indexAction()
{
}

public function sectionAction()
{
}

public function topicAction()
{
}

次に、 Default_Bootstrap 内に次のルートを作成しました。

$forumRoutes = new Zend_Controller_Router_Route(
    ':lang/:module/forum',
    array(
        'lang' => 'es',
        'module' => 'default',
        'controller' => 'forum',
        'action' => 'index'
    )
);

$sectionRoutes = new Zend_Controller_Router_Route(
    ':lang/:module/forum/:section',
    array(
        'lang' => 'es',
        'module' => 'default',
        'controller' => 'forum',
        'action' => 'section',
        'section' => ''
    )
);

$topic = new Zend_Controller_Router_Route(
    ':lang/:module/forum/:section/:topic',
    array(
        'lang' => 'es',
        'module' => 'default',
        'controller' => 'forum',
        'action' => 'topic',
        'section' => '',
        'topic' => ''
    )
);

$router->addRoute('forumTopics', $topic);
$router->addRoute('forumSections', $section);
$router->addRoute('forum', $forumRoutes);

これは、lang とモジュールを uri レベルで定義した場合にのみ機能しますが、次のように定義した場合は機能しません => mydomain/forum/section | セクション/トピック。これにより、ナビゲーション->メニューにも別の問題が発生します。ルーター定義で「フォーラム」を静的変数として定義した場合、navigatoin.xml で定義された任意のラベルにカーソルを合わせると、uri レベルはすべてのラベルで同じ値になります。

私はこのようなチェーンを作ろうとしました:

    $forumRoutes = new Zend_Controller_Router_Route(
        ':lang/:module/forum',
        array(
            'lang' => 'es',
            'module' => 'default',
            'controller' => 'forum',
            'action' => 'index'
        )
    );

    $section = new Zend_Controller_Router_Route(
        ':section',
        array(
            'action' => 'section',
            'section' => ''
        )
    )

    $topic = new Zend_Controller_Router_Route(
        ':topic',
        array(
            'action' => 'topic',
            'topic' => ''
        )
    )

    $chainedRoute = new Zend_Controller_Router_Route_Chain();
    $chainedRoute->chain($topic)
                 ->chain($section)
                 ->chain($forumRoutes);
    $router->addRoute($chainedRoute);

しかし、これは私が期待したようには機能しません。

どんな助けでも感謝します、ありがとう。

4

2 に答える 2

2

あなたは Zend の初心者です。あなたがそう言った。したがって、ここにいくつかの説明があります:

  1. 理想的には、Zend アプリケーションの URL は次のとおりです。

example.com/controller/action/param-name/:param-value

その場合、UsersController の下に Edit というアクションがあれば、次のようになります。

example.com/users/edit

アクションが追加の場合、次のようになります。

example.com/users/add

最初のパラメーターを変数として指定すると、コントローラーの要求と衝突します。例: コントローラーがユーザーであると言い、最初のパラメーターが値を受け入れてそれを従業員に入れる場合、example.com/employees および example.com/user としての要求は両方とも、ユーザーコントローラーが存在する場合でも従業員コントローラーを指します! これも理論です!

あなたがやりたいことは、ルーティングではなく動的な値のみを受け入れるようにルートを残すことです! ユーザーがアプリケーションをルーティングするのではなく、ユーザーをアプリケーションの別のセクションにルーティングします。

言語については、HTML 言語をチェックするZend_Localeを使用する必要があります。

<html lang="nl"> or <html lang = "en">

物事が明確であることを願っています!:)

于 2012-08-15T18:02:27.573 に答える
1

ZF でそのようなルートを操作するのに役立つ簡単な例を次に示します。

Zend Tool を使用して新しいプロジェクトを開始するときに得られるようなデフォルトのプロジェクト構造を使用している場合は、Application フォルダーに移動します。

Bootstrap.php で次のように設定します。

protected function _initRouteBind() {
        // get the front controller and get the router
        $front  = Zend_Controller_Front::getInstance();
        $router = $front->getRouter();
        // add each custom route like this giving them a descriptive name 
        $router->addRoute(
            'addTheDescriptiveRouteNameHere',
            new Zend_Controller_Router_Route(
                '/:controller/:id/:action/:somevar',
                array(
                    'module'     => 'default', 
                    'controller' => ':controller',
                    'action'     => ':action', 
                    'id'         => ':id',
                    'somevar'    => ':somevar'
                )
            )
        );   
}

上記の例は、コントローラー、アクション、およびいくつかのパラメーターが URL に設定されているルートを使用する方法を説明するためのものです。

コントローラーとアクションは、追加の作業なしで解決する必要がありますが、「id」または「somevar」値を取得するには、コントローラーでこれを実行できます。

public function yourAction()
{
    // How you get the parameters to pass in to a function
    $id = $this->getRequest()->getParam('id');
    $somevar = $this->getRequest()->getParam('somevar');

    // Using the parameters
    $dataYouWant = $this->yourAmazingMethod($id);
    $somethingElse = $this->yourOtherAmazingMethod($somevar);

    // Assign results to the view
    $this->view->data = $dataYouWant;
    $this->view->something = $somethingElse;
}     

したがって、メソッドが渡されるパラメーターを慎重に処理することを確認したい場合は (結局のところ、ユーザーが提供する情報です)、これがルート パラメーター バインディングを利用する背後にある原則です。もちろん、「/site」をルートとして CMS モジュールまたはコントローラーに直接送信し、次に「/site/:id」(「id」はページ識別子)に別のルートを指定することもできます。

また、次の良い代替手段を言うだけです:

$id = $this->getRequest()->getParam('id');
$somevar = $this->getRequest()->getParam('somevar');

アクションヘルパーと組み合わせて簡略化された条件文を使用すると、パラメーターが渡されると想定されていたため、とにかくそれ自体はそれほど良くありませんでした:

$id = ($this->_hasParam('id')) ? $this->_getParam('id') : null;
$somevar = ($this->_hasParam('somevar')) ? $this->_getParam('somevar') : null;

これに慣れていない場合は、

($this->_hasParam('id'))

true の場合は「:」の左側にあるものの値を割り当て、false の場合は「:」の右側にあるものの値を割り当てます。

true の場合、割り当てられる値は次のようになります。

$this->_getParam('id')

false の場合は null。それは役に立ちますか?:-D

于 2012-08-15T21:29:33.227 に答える