私は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'パラメータを使用してナビゲーション配列ノードを見つける必要があります。
編集
私は解決策を見つけました。答えは以下の通りです。