gpt4 book ai didi

php - 使用 ZendSkeletonModule 创建新模块 ZF3

转载 作者:行者123 更新时间:2023-12-03 00:54:04 24 4
gpt4 key购买 nike

我使用 ZendSkeletonModule 通过 git 创建了一个新模块:

git clone git://github.com/zendframework/ZendSkeletonModule.git Users

并根据新的模块名称进行了修改。但是,即使如此它也不起作用,我收到发生 404 错误

模块名称是User,以下是文件:

/module/Users/config/module.config.php
/module/Users/src/Module.php
/config/application.config.php
/config/modules.config.php
/composer.json

/module/Users/config/module.config.php

<?php
namespace Users;

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
'router' => [
'routes' => [
'users' => [
'type' => Literal::class,
'options' => [
// Change this to something specific to your module
'route' => '/users',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
// You can place additional routes that match under the
// route defined above here.
],
],
],
],
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
],
],
'view_manager' => [
'template_path_stack' => [
'Users' => __DIR__ . '/../view',
],
],
];

/module/Users/src/Module.php

<?php
/**
* @link http://github.com/zendframework/ZendSkeletonModule for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Users;

class Module
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
}

/config/application.config.php

<?php
/**
* If you need an environment-specific system or application configuration,
* there is an example in the documentation
* @see https://docs.zendframework.com/tutorials/advanced-config/#environment-specific-system-configuration
* @see https://docs.zendframework.com/tutorials/advanced-config/#environment-specific-application-configuration
*/
return [
// Retrieve list of modules used in this application.
'modules' => require __DIR__ . '/modules.config.php',

// These are various options for the listeners attached to the ModuleManager
'module_listener_options' => [
// This should be an array of paths in which modules reside.
// If a string key is provided, the listener will consider that a module
// namespace, the value of that key the specific path to that module's
// Module class.
'module_paths' => [
'./module',
'./vendor',
],

// An array of paths from which to glob configuration files after
// modules are loaded. These effectively override configuration
// provided by modules themselves. Paths may use GLOB_BRACE notation.
'config_glob_paths' => [
realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php',
],

// Whether or not to enable a configuration cache.
// If enabled, the merged configuration will be cached and used in
// subsequent requests.
'config_cache_enabled' => true,

// The key used to create the configuration cache file name.
'config_cache_key' => 'application.config.cache',

// Whether or not to enable a module class map cache.
// If enabled, creates a module class map cache which will be used
// by in future requests, to reduce the autoloading process.
'module_map_cache_enabled' => true,

// The key used to create the class map cache file name.
'module_map_cache_key' => 'application.module.cache',

// The path in which to cache merged configuration.
'cache_dir' => 'data/cache/',

// Whether or not to enable modules dependency checking.
// Enabled by default, prevents usage of modules that depend on other modules
// that weren't loaded.
// 'check_dependencies' => true,
],

// Used to create an own service manager. May contain one or more child arrays.
// 'service_listener_options' => [
// [
// 'service_manager' => $stringServiceManagerName,
// 'config_key' => $stringConfigKey,
// 'interface' => $stringOptionalInterface,
// 'method' => $stringRequiredMethodName,
// ],
// ],

// Initial configuration with which to seed the ServiceManager.
// Should be compatible with Zend\ServiceManager\Config.
// 'service_manager' => [],
];

/config/modules.config.php

<?php
/**
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* List of enabled modules for this application.
*
* This should be an array of module namespaces used in the application.
*/
return [
'Zend\ServiceManager\Di',
'Zend\Session',
'Zend\Mvc\Plugin\Prg',
'Zend\Mvc\Plugin\Identity',
'Zend\Mvc\Plugin\FlashMessenger',
'Zend\Mvc\Plugin\FilePrg',
'Zend\Mvc\I18n',
'Zend\Log',
'Zend\Form',
'Zend\Db',
'Zend\Cache',
'ZendDeveloperTools',
'Zend\Router',
'Zend\Validator',
'Application',
'Users',
];

/composer.json

{
"name": "zendframework/skeleton-application",
"description": "Skeleton Application for Zend Framework zend-mvc applications",
"type": "project",
"license": "BSD-3-Clause",
"keywords": [
"framework",
"mvc",
"zf"
],
"homepage": "http://framework.zend.com/",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^5.6 || ^7.0",
"zendframework/zend-component-installer": "^1.0 || ^0.3 || ^1.0.0-dev@dev",
"zendframework/zend-mvc": "^3.0.1",
"zfcampus/zf-development-mode": "^3.0",
"zendframework/zend-cache": "^2.7.1",
"zendframework/zend-db": "^2.8.1",
"zendframework/zend-mvc-form": "^1.0",
"zendframework/zend-json": "^3.0",
"zendframework/zend-log": "^2.9",
"zendframework/zend-mvc-i18n": "^1.0",
"zendframework/zend-mvc-plugins": "^1.0.1",
"zendframework/zend-psr7bridge": "^0.2.2",
"zendframework/zend-session": "^2.7.1",
"zendframework/zend-servicemanager-di": "^1.0"
},
"autoload": {
"psr-4": {
"Application\\": "module/Application/src/",
"Users\\": "module/Users/src/"
}
},
"autoload-dev": {
"psr-4": {
"ApplicationTest\\": "module/Application/test/"
}
},
"extra": [],
"scripts": {
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"development-disable": "zf-development-mode disable",
"development-enable": "zf-development-mode enable",
"development-status": "zf-development-mode status",
"post-create-project-cmd": [
"@development-enable"
],
"serve": "php -S 0.0.0.0:8080 -t public/ public/index.php",
"test": "phpunit"
},
"require-dev": {
"zendframework/zend-developer-tools": "^1.1.0",
"zendframework/zend-test": "^3.0.1"
}
}

问题更新

这是文件夹的屏幕截图以及/module/Users/src/Controller/IndexController.php的内容:

"/module/Users/src" and "/module/Users/view" folders

/module/Users/src/Controller/IndexController.php

<?php
/**
* @link http://github.com/zendframework/ZendSkeletonModule for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Users\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionController
{
public function indexAction()
{
return [];
}
}

问题更新 2 - 添加了一个文件

liga.vconf

 <VirtualHost *:80>
ServerName liga.localhost
DocumentRoot /users/cassiomc/sites/sistemas/liga/public
SetEnv APPLICATION_ENV "development"
<Directory /users/cassiomc/sites/sistemas/liga/public>
DirectoryIndex index.php
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

问题更新

这是当我尝试通过链接 liga.localhost/users 访问 Users 模块时出现的 404 屏幕的屏幕截图。

404 screen when try access Users module

问题已解决 - 添加文件以与旧文件以及新用户模块的“index.phtml”文件进行比较

/module/Users/config/module.config.php

<?php
namespace Users;

use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\Router\Http\Segment;

return [
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
],
],
'router' => [
'routes' => [
'users' => [
'type' => Segment::class,
'options' => [
'route' => '/users[/:action]',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
],
],
'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',
'users/index/index' => __DIR__ . '/../view/users/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
],
'template_path_stack' => [
__DIR__ . '/../view',
],
],
];

/module/Users/src/Module.php

<?php
/**
* @link http://github.com/zendframework/ZendSkeletonModule for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Users;

class Module
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
}

/config/application.config.php

<?php
/**
* If you need an environment-specific system or application configuration,
* there is an example in the documentation
* @see https://docs.zendframework.com/tutorials/advanced-config/#environment-specific-system-configuration
* @see https://docs.zendframework.com/tutorials/advanced-config/#environment-specific-application-configuration
*/
return [
// Retrieve list of modules used in this application.
'modules' => require __DIR__ . '/modules.config.php',

// These are various options for the listeners attached to the ModuleManager
'module_listener_options' => [
// This should be an array of paths in which modules reside.
// If a string key is provided, the listener will consider that a module
// namespace, the value of that key the specific path to that module's
// Module class.
'module_paths' => [
'./module',
'./vendor',
],

// An array of paths from which to glob configuration files after
// modules are loaded. These effectively override configuration
// provided by modules themselves. Paths may use GLOB_BRACE notation.
'config_glob_paths' => [
realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php',
],

// Whether or not to enable a configuration cache.
// If enabled, the merged configuration will be cached and used in
// subsequent requests.
'config_cache_enabled' => false,

// The key used to create the configuration cache file name.
'config_cache_key' => 'application.config.cache',

// Whether or not to enable a module class map cache.
// If enabled, creates a module class map cache which will be used
// by in future requests, to reduce the autoloading process.
'module_map_cache_enabled' => false,

// The key used to create the class map cache file name.
'module_map_cache_key' => 'application.module.cache',

// The path in which to cache merged configuration.
'cache_dir' => 'data/cache/',

// Whether or not to enable modules dependency checking.
// Enabled by default, prevents usage of modules that depend on other modules
// that weren't loaded.
// 'check_dependencies' => true,
],

// Used to create an own service manager. May contain one or more child arrays.
// 'service_listener_options' => [
// [
// 'service_manager' => $stringServiceManagerName,
// 'config_key' => $stringConfigKey,
// 'interface' => $stringOptionalInterface,
// 'method' => $stringRequiredMethodName,
// ],
// ],

// Initial configuration with which to seed the ServiceManager.
// Should be compatible with Zend\ServiceManager\Config.
// 'service_manager' => [],
];

/config/modules.config.php

<?php
/**
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* List of enabled modules for this application.
*
* This should be an array of module namespaces used in the application.
*/
return [
'Zend\Router',
'Zend\Validator',
'Application',
'Users'
];

/composer.json

{
"name": "zendframework/skeleton-application",
"description": "Skeleton Application for Zend Framework zend-mvc applications",
"type": "project",
"license": "BSD-3-Clause",
"keywords": [
"framework",
"mvc",
"zf"
],
"homepage": "http://framework.zend.com/",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^5.6 || ^7.0",
"zendframework/zend-component-installer": "^1.0 || ^0.3 || ^1.0.0-dev@dev",
"zendframework/zend-mvc": "^3.0.1",
"zfcampus/zf-development-mode": "^3.0"
},
"autoload": {
"psr-4": {
"Application\\": "module/Application/src/",
"Users\\": "module/Users/src/"
}
},
"autoload-dev": {
"psr-4": {
"ApplicationTest\\": "module/Application/test/"
}
},
"extra": [],
"scripts": {
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"development-disable": "zf-development-mode disable",
"development-enable": "zf-development-mode enable",
"development-status": "zf-development-mode status",
"post-create-project-cmd": [
"@development-enable"
],
"serve": "php -S 0.0.0.0:8080 -t public/ public/index.php",
"test": "phpunit"
}
}

这是文件夹的屏幕截图以及/module/Users/src/Controller/IndexController.php 的内容,现在代码正在运行:

Directory tree

/module/Users/src/Controller/IndexController.php

<?php
/**
* @link http://github.com/zendframework/ZendSkeletonModule for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Users\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionController
{
public function indexAction()
{
return [];
}
}

liga.vconf

 <VirtualHost *:80>
ServerName liga.localhost
DocumentRoot /users/cassiomc/sites/sistemas/liga/public
SetEnv APPLICATION_ENV "development"
<Directory /users/cassiomc/sites/sistemas/liga/public>
DirectoryIndex index.php
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

/module/Users/view/users/index/index.phtml

<strong>Module:</strong>        ZendSkeletonModule &raquo;
<strong>Controller:</strong> Skeleton &raquo;
<strong>Action:</strong> index

最佳答案

如果您安装了带有 Composer 的骨架应用程序和所有默认依赖项,请尝试以下操作:在 Composer.json 中添加此内容

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

并在命令行中运行以下命令

composer dump-autoload

我遇到了和你一样的问题,但是在完成这些步骤之后,模块正在运行。

这是创建新模块的逐步步骤 https://docs.zendframework.com/tutorials/getting-started/modules/

关于php - 使用 ZendSkeletonModule 创建新模块 ZF3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39421630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com