2

我用这篇文章中的信息制作了新插件:https ://luketowers.ca/blog/how-to-use-laravel-packages-in-october-cms-plugins/

我更新了 composer.php 并在供应商文件夹中创建了文件,我在后端看到插件 phpclasses/evalmath。

在页面上我尝试做数学运算:

function onStart() {

    // instantiate a new EvalMath

    $m = new EvalMath;

    $m->suppress_errors = true;

    // set the value of x

    $m->evaluate('x = 3');

    var_dump($m->evaluate('y = (x > 5)'));
}

我收到错误,找不到类“EvalMath”类在文件/plugins/phpclasses/evalmath/vendor/phpclasses/evalmath/evalmath.class.php 中定义我做错了什么?

在文件/plugins/phpclasses/evalmath/composer.json

{
  "require": {
    "phpclasses/evalmath": ">=1.0.0"
  },
  "repositories": [
    {
      "type": "composer",
      "url": "https:\/\/www.phpclasses.org\/"
    },
    {
      "packagist": false
    }
  ]
}

在文件/plugins/phpclasses/evalmath/Plugin.php

<?php namespace phpclasses\evalmath;

use App;
use Config;
use System\Classes\PluginBase;
use Illuminate\Foundation\AliasLoader;

/**
 *
 * Class Plugin */
class Plugin extends PluginBase
{
    /**
     *
     * Returns information about this plugin.
     * @return array
     */
    public function pluginDetails()
    {
        return ['name' => 'phpclasses/evalmath',

            'description' => 'OctoberCMS plugin for demonstrating the use of Laravel Packages within October plugins',

            'author' => 'hhh',

            'icon' => 'icon-leaf'
        ];
    }

    /**
     *
     * Runs right before the request route */
    public function boot()
    {
        // Setup required packages $this->bootPackages(); }
        /**
         *
         * Boots (configures and registers) any packages found within this plugin's packages.load configuration value
         * @see https://luketowers.ca/blog/how-to-use-laravel-packages-in-october-plugins
         * @author Luke Towers octobercms@luketowers.ca
         */
        public
        function bootPackages()
        { // Get the namespace of the current plugin to use in accessing the Config of the plugin $pluginNamespace = str_replace('\', '.', strtolower(NAMESPACE));

            // Instantiate the AliasLoader for any aliases that will be loaded
            $aliasLoader = AliasLoader::getInstance();

            // Get the packages to boot
            $packages = Config::get($pluginNamespace . '::packages');

            // Boot each package
            foreach ($packages as $name => $options) {
                // Setup the configuration for the package, pulling from this plugin's config
                if (!empty($options['config']) && !empty($options['config_namespace'])) {
                    Config::set($options['config_namespace'], $options['config']);
                }

                // Register any Service Providers for the package
                if (!empty($options['providers'])) {
                    foreach ($options['providers'] as $provider) {
                        App::register($provider);
                    }
                }

                // Register any Aliases for the package
                if (!empty($options['aliases'])) {
                    foreach ($options['aliases'] as $alias => $path) {
                        $aliasLoader->alias($alias, $path);
                    }
                }
            }
        }
    }
}

在文件/plugins/phpclasses/evalmath/classes/config.php

<?php 
return [

    // This contains the Laravel Packages that you want this plugin to utilize listed under their package identifiers
    'packages' => [

        'phpclasses/evalmath' => [

        ],

    ],
];
4

1 回答 1

0
  1. /plugins/phpclasses/evalmath/Plugin.php(bootPackages())如果您没有配置或其他提供程序或别名,则文件中的大部分代码都不是必需的

  2. 如果它是一个 laravel 包,您可以\App::register('\Your\LaravelPackage\ServiceProvider');在引导函数中使用该包向 laravel Provider 注册该包并为您的包添加一个别名 $alias = \Illuminate\Foundation\AliasLoader::getInstance()->alias('YourAlias', '\Your\LaravelPackage\Facade');

  3. 如果它不是 laravel 包,请尝试使用完整的命名空间我认为\EvalMath如果你使用这个包https://www.phpclasses.org/browse/file/11680.html

于 2017-10-29T03:06:50.277 回答