Zend-framework-creating-module

提供:Dev Guides
移動先:案内検索

Zend Framework-モジュールの作成

この章では、Zend FrameworkでMVCベースのモジュールを作成する方法を学びます。 モジュールの作成プロセスを理解するために、 Tutorial というモジュールを作成してみましょう。

 *–Module* という名前の新しいPHPクラスを–myapp/module/Tutorial/src/ディレクトリ内に作成し、ConfigProviderInterfaceを実装します。
* *Tutorial* を *Module* クラスのネームスペースとして設定します。
* *Module* クラスにパブリック関数 *getConfig* を記述し、 *Tutorial* モジュールの構成ファイルを返します。
  • モジュール*クラスの完全なコードは次のとおりです-
<?php
namespace Tutorial;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface {
   public function getConfig() {
      return include __DIR__ . '/../config/module.config.php';
   }
}

次のコードを使用して、 autoload セクションの下の composer.jsonTutorial モジュールを構成します。

"autoload": {
   "psr-4": {
      "Application\\": "module/Application/src/",
      "Tutorial\\": "module/Tutorial/src/"
   }
}

以下に示すようにcomposer update コマンドを使用してアプリケーションを更新します。

composer update
*composer* コマンドは、アプリケーションに必要な変更を行い、以下に示すようにコマンドプロンプトにログを表示します-
Loading composer repositories with package information
Updating dependencies (including require-dev)
   - Removing zendframework/zend-component-installer (0.3.0)
   - Installing zendframework/zend-component-installer (0.3.1)
   Downloading: 100%

   - Removing zendframework/zend-stdlib (3.0.1)
   - Installing zendframework/zend-stdlib (3.1.0)
   Loading from cache

   - Removing zendframework/zend-eventmanager (3.0.1)
   - Installing zendframework/zend-eventmanager (3.1.0)
   Downloading: 100%

   - Removing zendframework/zend-view (2.8.0)
   - Installing zendframework/zend-view (2.8.1)
   Loading from cache

   - Removing zendframework/zend-servicemanager (3.1.0)
   - Installing zendframework/zend-servicemanager (3.2.0)
   Downloading: 100%

   - Removing zendframework/zend-escaper (2.5.1)
   - Installing zendframework/zend-escaper (2.5.2)
   Loading from cache

   - Removing zendframework/zend-http (2.5.4)
   - Installing zendframework/zend-http (2.5.5)
   Loading from cache

   - Removing zendframework/zend-mvc (3.0.1)
   - Installing zendframework/zend-mvc (3.0.4)
   Downloading: 100%

   - Removing phpunit/phpunit (5.7.4)
   - Installing phpunit/phpunit (5.7.5)
   Downloading: 100%

Writing lock file
Generating autoload files

モジュール構成ファイル「module.config.php」を次のコードで /config/ に作成します-

<?php
namespace Tutorial;

use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\Router\Http\Segment;
return [
   'controllers' => [
      'factories' => [Controller\TutorialController::class => InvokableFactory::class,],
   ],
   'view_manager' => [
      'template_path_stack' => ['tutorial' => __DIR__ . '/../view',],
   ],
];

構成ファイルには3つの部分があり、それらは次のとおりです-

  • コントローラー構成-モジュール内で使用可能なコントローラーを指定します。
  • ルーティング構成-モジュール内のコントローラーをURLに解決する方法を指定します。
  • 構成の表示-ビューの場所など、エンジンの表示に関連する構成を指定します。

アプリケーションレベルの構成ファイル(myapp/config/modules.config.php)で Tutorial モジュールを構成します。

return ['Zend\Router', 'Zend\Validator', 'Application', 'Tutorial'];

アプリケーションフォルダーのルートで composer serve を実行して、アプリケーションを実行します。

新しいモジュールは正常に追加されましたが、 Tutorial モジュールを正常に実行するには、 Controller、Routing および Views を追加する必要があります。