草庐IT

php - '在插件管理器 Zend\Router\RoutePluginManager 中找不到名为 "Example\Segment"的插件

coder 2024-04-23 原文

我目前正在学习 Zend 3 教程。唯一的区别是我使用的是示例表/类而不是相册。一切似乎都符合规范,除了我每次尝试访问开发服务器时都会收到以下错误:

Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'A plugin by the name "Example\Segment" was not found in the plugin manager Zend\Router\RoutePluginManager' in C:\Websites\ZEND2\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php:133 Stack trace: #0 C:\Websites\ZEND2\vendor\zendframework\zend-router\src\SimpleRouteStack.php(280): Zend\ServiceManager\AbstractPluginManager->get('Example\Segment', Array) #1 C:\Websites\ZEND2\vendor\zendframework\zend-router\src\Http\TreeRouteStack.php(201): Zend\Router\SimpleRouteStack->routeFromArray(Array) #2 C:\Websites\ZEND2\vendor\zendframework\zend-router\src\Http\TreeRouteStack.php(151): Zend\Router\Http\TreeRouteStack->routeFromArray(Array) #3 C:\Websites\ZEND2\vendor\zendframework\zend-router\src\SimpleRouteStack.php(142): Zend\Router\Http\TreeRouteStack->addRoute('example', Array) #4 C:\Websites\ZEND2\vendor\zendframework\zend-router\src\SimpleRouteStack.php(86): Zend\Router\SimpleRouteStack->addRoutes(Array) in C:\Websites\ZEND2\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php on line 133

ExampleController.php

namespace Example\Controller;

use Example\Model\ExampleTable;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class ExampleController extends AbstractActionController
{
    private $table;

    public function indexAction()
    {
        return new ViewModel([
            'examples' => $this->table->fetchAll(),
        ]);
    }

    public function addAction()
    {
    }

    public function editAction()
    {
    }

    public function deleteAction()
    {
    }

     public function __construct(ExampleTable $table)
    {
        $this->table = $table;
    }
}

ExampleTable.php

<?php 

namespace Example\Model;

use RuntimeException;
use Zend\Db\TableGateway\TableGatewayInterface;

class ExampleTable
{
    private $tableGateway;

    public function __construct(TableGatewayInterface $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll()
    {
        return $this->tableGateway->select();
    }

    public function getExample($id)
    {
        $id = (int) $id;
        $rowset = $this->tableGateway->select(['id' => $id]);
        $row = $rowset->current();
        if (! $row) {
            throw new RuntimeException(sprintf(
                'Could not find row with identifier %d',
                $id
            ));
        }

        return $row;
    }

    public function saveExample(Example $example)
    {
        $data = [
            'name' => $example->name,
            'description'  => $example->description,
            'web_url'  => $example->web_url,
        ];

        $id = (int) $example->id;

        if ($id === 0) {
            $this->tableGateway->insert($data);
            return;
        }

        if (! $this->getExample($id)) {
            throw new RuntimeException(sprintf(
                'Cannot update example with identifier %d; does not exist',
                $id
            ));
        }

        $this->tableGateway->update($data, ['id' => $id]);
    }

    public function deleteExample($id)
    {
        $this->tableGateway->delete(['id' => (int) $id]);
    }
}

模块.config.php

<?php

namespace Example;

return [

    // The following section is new and should be added to your file:
    'router' => [
        'routes' => [
            'example' => [
                'type'    => Segment::class,
                'options' => [
                    'route' => '/example[/:action[/:id]]',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => Controller\ExampleController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
        ],
    ],

    'view_manager' => [
        'template_path_stack' => [
            'example' => __DIR__ . '/../view',
        ],
    ],
];

模块.php

<?php

namespace Example;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ConfigProviderInterface;

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

    // Add this method:
    public function getServiceConfig()
    {
        return [
            'factories' => [
                Model\ExampleTable::class => function($container) {
                    $tableGateway = $container->get(Model\ExampleTableGateway::class);
                    return new Model\ExampleTable($tableGateway);
                },
                Model\ExampleTableGateway::class => function ($container) {
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Model\Example());
                    return new TableGateway('example', $dbAdapter, null, $resultSetPrototype);
                },
            ],
        ];
    }

     public function getControllerConfig()
    {
        return [
            'factories' => [
                Controller\ExampleController::class => function($container) {
                    return new Controller\ExampleController(
                        $container->get(Model\ExampleTable::class)
                    );
                },
            ],
        ];
    }
}

例子.php

namespace Example\Model;

class Example
{
    public $id;
    public $name;
    public $description;
    public $web_url

    public function exchangeArray(array $data)
    {
        $this->id     = !empty($data['id']) ? $data['id'] : null;
        $this->name = !empty($data['name']) ? $data['name'] : null;
        $this->description  = !empty($data['description']) ? $data['description'] : null;
        $this->web_url  = !empty($data['web_url']) ? $data['web_url'] : null;
    }
}

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\Form',
    'Zend\Db',
    'Zend\Router',
    'Zend\Validator',
    'Application',
    'Example',
];

我已经包含了所有内容,因为我真的不确定问题出在哪里。任何帮助将不胜感激。

最佳答案

您的 module.config.php 文件需要:

use Zend\Router\Http\Segment;

靠近顶部的声明,用于创建稍后在文件中使用的 Segment 别名。目前,因为它不存在,PHP 正在您声明的命名空间(所以 Example\Segment)中寻找那个不存在的类。

关于php - '在插件管理器 Zend\Router\RoutePluginManager 中找不到名为 "Example\Segment"的插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38470511/

有关php - '在插件管理器 Zend\Router\RoutePluginManager 中找不到名为 "Example\Segment"的插件的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  4. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  5. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  6. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  7. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  8. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  9. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  10. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

随机推荐