Skip to content
Advertisement

How to design a simple plugin based on pico CMS without using required_once?

I’m hoping to design a simple plugin based on pico CMS and call it in a protected function in my main class, called App.

Plugin is called AppHelper.

Attempt

I added a required_once on the top of App class:

JavaScript

and tested using this (which first AppHelper::isMobile is not right):

JavaScript

in a protected method:

JavaScript

and works okay with just a warning. However, this is not right.

How do I correctly design/add this plugin without required_once and autoload it using composer install, like all my other plugins that function so?

AppHelper

JavaScript

AbstractAppPlugin

JavaScript

Advertisement

Answer

I don’t see any namespace in your classes? Can you add how the classes map to the filesystem? There is a PHP-standard called PSR-4 which is commonly used. If this does not meet your requirements, composer offers a few alternative autoloading options that you can specify in your plugin’s composer.json.

See: https://getcomposer.org/doc/04-schema.md#autoload

For example if your plugin only consists of this one AppHelper class that is kept in a file src/AppHelper.php and the abstract base class you could just add a classmap with those 2 files to your composer.json:

JavaScript

Since both classes map to the classes this would work just as well with PSR-0:

JavaScript

This will add all the php files in the src directory to a corresponding class, so:

JavaScript

and so on. You might run into trouble though as you are mapping into the global namespace. Using a custom namespace for those classes instead is probably a better choice and usually can be done quickly with the help of some tools like the refactoring helpers in PhpStorm.

edit: Sorry, I missed the link to the actual repository. In your case it should probably look like:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement