3

私はZendFramework2を初めて使用し、このフレームワークを学びたいと思っています。ルーターにURLエイリアスを作成したい。たとえば、module.config.phpでこのようなものを定義しました

'router' => array(
    'routes' => array(

        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),
        'node' => array(
            'type'    => 'Application\Controller\AliasSegment',
            'options' => array(
                'route'    => '/node[/:id]',
                'constraints' => array(
                    'id' => '[0-9]+'
                ),
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                    'id'            => '0'
                ),
            ),
            'may_terminate' => true,
        ),

    ),
),

入力www.myapp.local/node/1すると、アプリケーションのデフォルトコントローラーのデフォルトアクションにルーティングされます。私が欲しいのは、URLパスのエイリアスを処理できるルーター拡張機能です。例えば:

www.myapp.local/node/1 = www.myapp.local/aboutus
www.myapp.local/node/2 = www.myapp.local/company/gallery

私はそれがZFで可能だったことを知っています。これは、ZFでこれを実現する方法のチュートリアルへのリンクです。 フレンドリーなURL これはポーランド語であることは知っていますが、コードは自明だと思います:)

アイデアは、URLヘルパーを使用して、エイリアスまたは通常のセグメント(node / [:id])を使用して有効なURLを組み立てることです。

Application \ ControllerフォルダーにAliasSegmentクラスを既に作成しましたが、エラーが表示されます。

Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Application\Controller\AliasSegment' in C:\xampp\htdocs\industengine\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:450 Stack trace: #0 

私のAliasSegmentクラス(不完全):

    <?php

    namespace Zend\Mvc\Router\Http;

    use Traversable;
    use Zend\Mvc\Router\Exception;
    use Zend\Stdlib\ArrayUtils;
    use Zend\Stdlib\RequestInterface as Request;

 class AliasSegment extends Segment
 {


    public function match(Request $request, $pathOffset = null)
    {


    }

 }

私は何時間も答えを探していましたが、何も見つかりませんでした。少なくとも私が間違っていること、コードを挿入する場所、またはより良い解決策を知っているかどうかを教えてください。

すぐに使えるアプリケーションを探していません。私は何かを学びたいのですが、詳細に答えを教えていただければ幸いです:)

事前に感謝し、私の英語をお詫びします:)

編集:

私のカスタムルーターは現在機能しています。現時点では、エイリアスはハードコーディングされていますが、機能します。

私のAliasSegmentクラスは次のようになります。

<?php

namespace Application\Controller;

use Traversable;
use Zend\Mvc\Router\Exception;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Mvc\Router\Http;

class AliasSegment extends \Zend\Mvc\Router\Http\Segment
{


    public function match(Request $request, $pathOffset = null)
    {
        $uri  = $request->getUri();
        $path = $uri->getPath();

        //sample logic here
        //for /about/gallery uri set node id to 1
        //todo: get action, controller and module from navigation        
        if($path == '/about/gallery'){
            $uri->setPath('/node/1');
            $request->setUri($uri);
        }

        return parent::match($request, $pathOffset);

    }

    protected function buildPath(array $parts, array $mergedParams, $isOptional, $hasChild)
    {

        if(isset($mergedParams['link'])){
            return $mergedParams['link'];
        }

        return parent::buildPath($parts, $mergedParams, $isOptional, $hasChild);
    }

}

この場合/about/gallery、はのエイリアス/node/1です。どちらのアドレスも正しいです。buildPath関数は、エイリアスパスを正しく返します。まあ、これが誰かに役立つことを願っています:)

ただし、Zend_Navigationで「link」という名前の追加パラメーターを使用してセットアップしたいと思います。

達成したいことの50%を実行しましたが、ルーターからZend_Navigationを取得するのに問題があります。どうやって渡すのかわからない。私はそれがこのようなものでなければならないと思います:

$sm = $this->getServiceLocator();
$auth = $sm->get('Navigation');

IndexControllerでは機能しますが、AliasSegmentでは機能しません。'link'パラメータを使用してナビゲーション配列ノードを見つける必要があります。

編集

私は解決策を見つけました。答えは以下の通りです。

4

3 に答える 3

2

Application \ Controller\AliasSegmentのインスタンスをフェッチまたは作成できません

これがコントローラーの場合、module.config.phpで次のようになると思います。

'controllers' => array(
    'invokables' => array(
        '\Application\Controller\AliasSegment' => '\Application\Controller\AliasSegment',
    )
),

また、クラスの名前空間は少し奇妙に見えます。

名前空間Zend\Mvc \ Router \ Http;

どうですか:

namespace Application\Controller;
于 2012-11-10T00:24:47.880 に答える
1

OK、できました。このスレッドにとって重要なこと: ZF2:カスタムルート内でZend \ Navigationを取得する方法は?

任意のセグメントタイプのルートを使用できます。ただし、これを機能させるには少し変更が必要な場合がありmatchます。

ナビゲーションの単一ページに「link」パラメータがある場合、URLは「link」文字列に変換されますが、他のパラメータはその背後に残ります。現在のルートのデフォルトURIのオーバーレイと考えてください。

カスタムルートクラスを少し変更する必要がありました。まず、名前空間をに変更する必要がありましたApplication\Router。これがフルクラスです:

// EDIT - file within ModuleName/src/Router/Alias.php
namespace Application\Router;

use Traversable;
use Zend\Mvc\Router\Exception;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Mvc\Router\Http;

class Alias extends Http\Segment
{
    private static $_navigation = null;

    public function match(Request $request, $pathOffset = null)
    {
        $uri  = $request->getUri();
        $path = $uri->getPath();


        $items = self::$_navigation->findAllBy('route', 'node');
        $params = null;

        if($items != null){
            $t = sizeof($items);
            for ($i=0; $i < $t; $i++) { 
                $item = $items[$i];
                $params = $item->getParams();
                if (isset($params['link']) && $params['link']==$path){
                    $uri->setPath('/'.$item->getRoute().'/'.$params['id']);
                    $request->setUri($uri);
                    break;
                }
            }
        }

        return parent::match($request, $pathOffset);
    }

    public function setNavigation($navigation){
        self::$_navigation = $navigation;
    }

    protected function buildPath(array $parts, 
                                 array $mergedParams, $isOptional, $hasChild)
    {

        if(isset($mergedParams['link'])){
            return $mergedParams['link'];
        }

        return parent::buildPath($parts, $mergedParams, 
                                               $isOptional, $hasChild);
    }

}

これがのサンプル部分ですmodule.config.php

'navigation' => array(
        // The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key
        'default' => array(
            // And finally, here is where we define our page hierarchy
            'account' => array(
                'label' => 'Account',
                'route' => 'node',
                'params' => array(
                            'id' => '2',
                            ),
                'pages' => array(
                    'home' => array(
                        'label' => 'Dashboard',
                        'route' => 'node',
                        'params' => array(
                                    'id' => '8',
                                    'link' => '/about/gallery'
                                    ),

                    ),
                    'login' => array(
                        'label' => 'Sign In',
                        'route' => 'node',
                        'params' => array(
                                    'id' => '6',
                                    'link' => '/signin'
                                    ),

                    ),
                    'logout' => array(
                        'label' => 'Sign Out',
                        'route' => 'node',
                        'params' => array(
                                    'id' => '3',
                                    ),
                    ),
                ),
            ),
        ),
    ),
    'router' => array(
        'routes' => array(

            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            ),
            'node' => array(
                'type'    => 'Application\Router\Alias',
                'options' => array(
                    'route'    => '/node[/:id]',
                    'constraints' => array(
                        'id' => '[0-9]+'
                    ),
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                        'id'            => '0'
                    ),
                ),
                'may_terminate' => true,
            ),

        ),
    ),
于 2012-11-11T15:04:02.907 に答える
0

のようなルート専用の場合は/about/about/galery子ルートでリテラルルートを使用できます

'about' => array(
    'type' => 'literal',
    'options' => array(
        'route' => '/about',
        'defaults' => array(
            'controller' => 'module-controller-about',
            'action'     => 'index'
        )
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'galery' => array(
            'type' => 'literal',
            'options' => array(
                'route' => '/galery',
                'defaults' => array(
                    'controller' => 'module-controller-galery'
                )
            )
        )
    )
)

/blog/1-my-great-seo-titleあなたがおそらく正規表現ルートを設定しなければならないようなURLに関しては(これは最も遅く、リテラルは最も速いです)。

多分彼のルータープレゼンテーションからDASPRiDsスライドをチェックしてください

于 2012-11-10T08:48:27.960 に答える