This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

JimRed
JimRed

Hello Guys,

Could you please help me?
How can I create my own global classes and functiions in October CMS?
The "global" means classes and functions that I can call everywhere on pages and layouts without repeating them on every place where I should use them?

Thank you for your help,
Imre

Last updated

JimRed
JimRed

No one can help me? :(

axomat
axomat

Try installing October and writing a plugin, these can be used on any page and support your own layouts, parameters and so on.

If this is not what you want then I think you are not looking for a CMS. Have a look at Laravel instead, it is what October is written on top of.

erm3nda
erm3nda

The "global" means classes and functions that I can call everywhere on pages and layouts without repeating them on every place

The way to call a function on every page render, is to put the call on the right site, nothing related to be global or not or the function design. Call that function from the layout "as example", and you will be able to use it's (static or not) functions/methods from any piece of code you call below.

"without repeating them" that makes me confused about what are you looking for.

daftspunky
daftspunky

In PHP:

\System\Classes\MarkupManager::instance()->registerCallback(function($manager){
    $manager->registerFunctions([
        'myFunction' => function($param1){
            return "Hello " . $param1;
        },
    ]);
]);

In Twig:

<!-- This will output: Hello world! -->
{{ myFunction('world!') }}

More info: http://octobercms.com/docs/plugin/registration#extending-twig

Last updated

adis.osmonov
adis.osmonov
\System\Classes\MarkupManager::instance()->registerCallback(function($manager){
    $manager->registerFunctions([
        'myFunction' => function($param1){
            return "Hello " . $param1;
        },
    ]);
});

Insert curly bracket before last round bracket instead square.

Last updated

oskar.villani40843
oskar.villani40843

Hello,

when I am using the code above (inside the onInit() or onStart() function of my layout) the function 'myFunction' can be used by twig.

But how is this function to be called within the php code of my page?

JimRed
JimRed

oskar.villani40843 said:

Hello,

when I am using the code above (inside the onInit() or onStart() function of my layout) the function 'myFunction' can be used by twig.

But how is this function to be called within the php code of my page?

Hi Oskar,

I suggest you to move the function to your custom class so...

  • you can call this static methods everywhere in the php code section of your cms pages
  • you can specify the same method to extend twig markup functions
  • you can keep your code clearer and modular

How to do this exactly?

  1. Create your own Plugin.
    There are countless way to create your own plugin in OctoberCMS but if you don't know how to do this than I think you should use Builder plugin which makes plugin creation ease (read its documentation for details).
    https://octobercms.com/plugin/rainlab-builder
    Of course if you've already used the script above you probably know how to create plugin but this info can be useful for beginners who read this post in future.

  2. Create a custom class inside your plugin folder
    Open your plugin folder (for example in: ./plugins/oskar/tutorial ...where oskar is your author name and tutorial is the name of the plugin) and create a "classes" folder inside your plugin folder (./plugins/oskar/tutorial/classes). In the "classes" folder create a new PHP file called for example "MyClass.php" with this content:

    <?php
    namespace Oskar\Tutorial\Classes;
    class MyClass
    {
        public static function myMethod($param1)
        {
            return "Hello " . $param1;
        }
    }

    Note: keep in mind that you have to change namespace according to your real author and plugin name also the name of the file need to match the class name in it (oh and don't forget that namespace and class name is case sensitive)..

  3. Extend Twig markup language with the new function
    Open to edit your "Plugin.php" inside your plugin folder (./plugins/oskar/tutorial/Plugin.php) and add the following code:

    public function registerMarkupTags()
    {
        return [
            'functions' => [
                'myFunction' => ['\Oskar\Tutorial\Classes\MyClass', 'myMethod'],
            ]
        ];
    }

    Note: Again, don't forget to change namespace according to your needs.

You've done!

Now you can use this function in Twig like you did it before:

<!-- This will output: Hello world! -->
{{ myFunction('world!') }}

but you also can call the same method in the php section of your cms page:

use Oskar\Tutorial\Classes\MyClass;
function onStart()
{
    echo MyClass::myMethod('world!');    
}

That's all.
I hope you will be able to solve your problem....
Wish you the bests.

Last updated

oskar.villani40843
oskar.villani40843

Hello mail.imre.veres452 !

1000 thanks for this detailed and for me (as a non-expert) understandable description. I really love it and it solves my problem! Especially because I've already built my plugin with builder, so I only have to follow your tips to get the achieved result.

May be you like to post this how-to on https://octobertricks.com/ ? It's an another helpful page for octoberCMS I found recently and fine to spread the word, I think!

Best regards & cheers :)

Last updated

oskar.villani40843
oskar.villani40843

Found an interesting behaviour of octoberCMS - or is this quite usual?

When my path to my class is ./plugins/oskar/tutorial/classes (notice: all in small letters) then usually I write: use Oskar\Tutorial\Classes... for the path with capital initial letter. But the name of the class file itself has to be written in the 'use' statement as the .php file is written.

// if class php file is named MyClass.php 
use Oskar\Tutorial\Classes\MyClass
// if class php file is named myclass.php
use Oskar\Tutorial\Classes\myclass
JimRed
JimRed

oskar.villani40843 said:

Found an interesting behaviour of octoberCMS - or is this quite usual?

When my path to my class is ./plugins/oskar/tutorial/classes (notice: all in small letters) then usually I write: use Oskar\Tutorial\Classes... for the path with capital initial letter. But the name of the class file itself has to be written in the 'use' statement as the .php file is written.

// if class php file is named MyClass.php use Oskar\Tutorial\Classes\MyClass // if class php file is named myclass.php use Oskar\Tutorial\Classes\myclass

Hi Oskar,

I'm glad to have been able to help you and of course I will check the site you've linked and I will post the tutorial there too.

The mentioned behaviour regarding case-sensitivity is not related to OctoberCMS itself but to PHP - however it works like this because of the default settings of class autoloading in OctoberCMS as it uses PSR-4 naming conventions.

OctoberCMS autoloads classes from all "plugins/..author../..pluginname../classes" (base) directory automaticaly so they are available instantly but you always have to keep in mind the PSR-4 rules. (I've pointed it in my tutorial above too ;))

You can see details here: https://www.php-fig.org/psr/psr-4/

You can use any kind of letters in the path part of the namespace because that part ("Oskar\Tutorial\Classes") is the "base directory" for autoload but under "classes" folder you will need to use capital folder/file names then you have to use capital folders/files in the namespace definitions too.

ps.: If you would use IIS (or any other Windows based system) in the future then you will find that it is NOT case-sensitive (it finds the file even if it does not match capital letters) but I suggest to always use PSR-4 rules even on IIS because you will face some serious issues in case you would move and migrate your code to Linux based environment - trust me it's personal experience. :)

Farzin.b
Farzin.b

mail.imre.veres452 said:

oskar.villani40843 said:

Hello,

when I am using the code above (inside the onInit() or onStart() function of my layout) the function 'myFunction' can be used by twig.

But how is this function to be called within the php code of my page?

Hi Oskar,

I suggest you to move the function to your custom class so...

  • you can call this static methods everywhere in the php code section of your cms pages
  • you can specify the same method to extend twig markup functions
  • you can keep your code clearer and modular

How to do this exactly?

  1. Create your own Plugin.
    There are countless way to create your own plugin in OctoberCMS but if you don't know how to do this than I think you should use Builder plugin which makes plugin creation ease (read its documentation for details).
    https://octobercms.com/plugin/rainlab-builder
    Of course if you've already used the script above you probably know how to create plugin but this info can be useful for beginners who read this post in future.

  2. Create a custom class inside your plugin folder
    Open your plugin folder (for example in: ./plugins/oskar/tutorial ...where oskar is your author name and tutorial is the name of the plugin) and create a "classes" folder inside your plugin folder (./plugins/oskar/tutorial/classes). In the "classes" folder create a new PHP file called for example "MyClass.php" with this content:

    <?php
    namespace Oskar\Tutorial\Classes;
    class MyClass
    {
       public static function myMethod($param1)
       {
           return "Hello " . $param1;
       }
    }

    Note: keep in mind that you have to change namespace according to your real author and plugin name also the name of the file need to match the class name in it (oh and don't forget that namespace and class name is case sensitive)..

  3. Extend Twig markup language with the new function
    Open to edit your "Plugin.php" inside your plugin folder (./plugins/oskar/tutorial/Plugin.php) and add the following code:

    public function registerMarkupTags()
    {
       return [
           'functions' => [
               'myFunction' => ['\Oskar\Tutorial\Classes\MyClass', 'myMethod'],
           ]
       ];
    }

    Note: Again, don't forget to change namespace according to your needs.

You've done!

Now you can use this function in Twig like you did it before:

<!-- This will output: Hello world! -->
{{ myFunction('world!') }}

but you also can call the same method in the php section of your cms page:

use Oskar\Tutorial\Classes\MyClass;
function onStart()
{
   echo MyClass::myMethod('world!');    
}

That's all.
I hope you will be able to solve your problem....
Wish you the bests.

Thanks for your complete description.

1-12 of 12

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.