はじめに
お仕事で、Zend Frameworkのバージョンアップをしなければならなくなった・・と思ったら、Zend Frameworkはもうなくて、Laminas Projectに移って新たなフレームワークとして公開されている。
いちから勉強しないといけないじゃん、ということで、必要な要件を満たせるかどうかを一歩ずつ調査していく。
要件(3)
3つ目の要件は、「forwardとredirectができること」。
これはまぁ、機能としてはあるんだろうから、要件というよりやり方の調査という側面が大きいけど。
導入
こちらでセットアップした環境を(コピーして)使っていく。
私は以下のようにコピーを作成。
cp -pr laminas-setup-2-separated-views laminas-setup-3-forward-redirect cd laminas-setup-3-forward-redirect
設定・実装
module/Application/config/module.config.phpに以下の記述を追加する。
:
'router' => [
'routes' => [
:
'forwarded' => [
'type' => Segment::class,
'options' => [
'route' => '/forwarded[/:action]',
'defaults' => [
'controller' => Controller\ForwardedController::class,
'action' => 'index',
],
],
],
'redirected' => [
'type' => Segment::class,
'options' => [
'route' => '/redirected[/:action]',
'defaults' => [
'controller' => Controller\RedirectedController::class,
'action' => 'index',
],
],
],
],
],
'controllers' => [
'factories' => [
:
Controller\ForwardedController::class => InvokableFactory::class,
Controller\RedirectedController::class => InvokableFactory::class,
],
],
'view_manager' => [
:
'template_map' => [
:
'application/index/chrome' => __DIR__ . '/../view/application/index/chrome.tpl',
'application/index/firefox' => __DIR__ . '/../view/application/index/firefox.tpl',
:
],
:
],
];
追加後の内容は以下のような感じ。
<?php declare(strict_types=1); namespace Application; use Laminas\Router\Http\Literal; use Laminas\Router\Http\Segment; use Laminas\ServiceManager\Factory\InvokableFactory; return [ 'router' => [ 'routes' => [ 'home' => [ 'type' => Literal::class, 'options' => [ 'route' => '/', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'index', ], ], ], 'application' => [ 'type' => Segment::class, 'options' => [ 'route' => '/application[/:action]', 'defaults' => [ 'controller' => Controller\IndexController::class, 'action' => 'index', ], ], ], 'forwarded' => [ 'type' => Segment::class, 'options' => [ 'route' => '/forwarded[/:action]', 'defaults' => [ 'controller' => Controller\ForwardedController::class, 'action' => 'index', ], ], ], 'redirected' => [ 'type' => Segment::class, 'options' => [ 'route' => '/redirected[/:action]', 'defaults' => [ 'controller' => Controller\RedirectedController::class, 'action' => 'index', ], ], ], ], ], 'controllers' => [ 'factories' => [ Controller\IndexController::class => InvokableFactory::class, Controller\ForwardedController::class => InvokableFactory::class, Controller\RedirectedController::class => InvokableFactory::class, ], ], 'view_manager' => [ 'display_not_found_reason' => true, 'display_exceptions' => true, 'doctype' => 'HTML5', 'not_found_template' => 'error/404', 'exception_template' => 'error/index', 'template_map' => [ 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 'application/index/index' => __DIR__ . '/../view/application/index/index.tpl', 'application/index/chrome' => __DIR__ . '/../view/application/index/chrome.tpl', 'application/index/firefox' => __DIR__ . '/../view/application/index/firefox.tpl', 'error/404' => __DIR__ . '/../view/error/404.phtml', 'error/index' => __DIR__ . '/../view/error/index.phtml', ], 'template_path_stack' => [ __DIR__ . '/../view', ], 'strategies' => [ 'Smarty\\View\\Strategy', ], ], ];
module/Application/src/Controller/IndexController.phpを以下のように修正する。redirectActionメソッドとforwardActionメソッドを追加している。
<?php declare(strict_types=1); namespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; class IndexController extends AbstractActionController { public function indexAction() { $timeStr = date('Y/m/d H:i:s'); $isFirefox = preg_match('/Firefox/', $_SERVER['HTTP_USER_AGENT']); return new ViewModel([ 'time_str' => $timeStr, 'user_agent' => $_SERVER['HTTP_USER_AGENT'], 'is_firefox' => $isFirefox, ]); } public function redirectAction() { $isFirefox = preg_match('/Firefox/', $_SERVER['HTTP_USER_AGENT']); if ($isFirefox) { return $this->redirect()->toRoute('redirected', ['action' => 'firefox']); } else { return $this->redirect()->toRoute('redirected', ['action' => 'chrome']); } } public function forwardAction() { $isFirefox = preg_match('/Firefox/', $_SERVER['HTTP_USER_AGENT']); if ($isFirefox) { $forward = $this->forward()->dispatch(ForwardedController::class, ['action' => 'firefox']); } else { $forward = $this->forward()->dispatch(ForwardedController::class, ['action' => 'chrome']); } return $forward; } }
module/Application/src/Controller/ForwardedController.phpを以下の内容で作成する。
<?php declare(strict_types=1); namespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; class ForwardedController extends AbstractActionController { public function indexAction() { $timeStr = date('Y/m/d H:i:s'); $isFirefox = preg_match('/Firefox/', $_SERVER['HTTP_USER_AGENT']); return new ViewModel([ 'time_str' => $timeStr, 'user_agent' => $_SERVER['HTTP_USER_AGENT'], 'is_firefox' => $isFirefox, ]); } public function chromeAction() { return new ViewModel(); } public function firefoxAction() { return new ViewModel(); } }
module/Application/src/Controller/RedirectedController.phpを以下の内容で作成する。
<?php declare(strict_types=1); namespace Application\Controller; use Laminas\Mvc\Controller\AbstractActionController; use Laminas\View\Model\ViewModel; class RedirectedController extends AbstractActionController { public function indexAction() { $timeStr = date('Y/m/d H:i:s'); $isFirefox = preg_match('/Firefox/', $_SERVER['HTTP_USER_AGENT']); return new ViewModel([ 'time_str' => $timeStr, 'user_agent' => $_SERVER['HTTP_USER_AGENT'], 'is_firefox' => $isFirefox, ]); } public function chromeAction() { return new ViewModel(); } public function firefoxAction() { return new ViewModel(); } }
ビューのテンプレートファイルを作成する。
module/Application/view/application/forwarded/chrome.tpl
Forwarded Chrome view
module/Application/view/application/forwarded/firefox.tpl
Forwarded Firefox view
module/Application/view/application/redirected/chrome.tpl
Redirected Chrome view
module/Application/view/application/redirected/firefox.tpl
Redirected Firefox view
実行
Firefoxでhttp://192.168.56.xxx/laminas-setup-3-forward-redirect/public/application/forwardにアクセスする。
URLはそのままで、以下の画面が出ればOK。

Firefoxでhttp://192.168.56.xxx/laminas-setup-3-forward-redirect/public/application/redirectにアクセスする。
URLがhttp://192.168.56.xxx/laminas-setup-3-forward-redirect/public/redirected/firefoxに変わって以下の画面が出ればOK。

Google Chromeでhttp://192.168.56.xxx/laminas-setup-3-forward-redirect/public/application/forwardにアクセスする。
URLはそのままで、以下の画面が出ればOK。

Google Chromeでhttp://192.168.56.xxx/laminas-setup-3-forward-redirect/public/application/redirectにアクセスする。
URLがhttp://192.168.56.xxx/laminas-setup-3-forward-redirect/public/redirected/chromeに変わって以下の画面が出ればOK。

まとめ
- 同一モジュール内であれば、同一コントローラー、別コントローラーでもforward、redirectができることを確認
- 別モジュールの場合は、いろいろ試したが、forward、redirectはできなさそう
- もしかしたらやり方があるのかもしれないけど、今のところ見つけられず