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

KurtJensen
KurtJensen

Trying to put a trait in my plugin so I cam share common code between my components.

  • I created a Trait in october/plugins/kurtjensen/myplugin/traits/MyTrait.php

    <?php namespace KurtJensen\MyPlugin\Traits;
    use Auth;
    use RainLab\User\Components\Account;
    use KurtJensen\MyPlugin\Models\Settings;
    trait MyTrait
    {
        public static $myarray = [];
       public function myFunction()
        {
                   return array(1,2,3,4);
        }
    }
  • In my component october/plugins/kurtjensen/myplugin/components/MyComponent.php I did:

    <?php namespace KurtJensen\MyPlugin\Components;
    use RainLab\Blog\Components\Post;
    class MyComponent extends Post
    {
        use MyTrait;
    // mycode
    }

I am getting error:

    Trait 'KurtJensen\MyPlugin\Components\MyTrait' not found

I have tried changing namespace and moving traits folder around inside plugin but nothing I have tried seems to get this working. if I use:

     use KurtJensen\MyPlugin\MyTrait;

I get:

    Trait 'KurtJensen\MyPlugin\Components\KurtJensen\MyPlugin\MyTrait' not found

if I use:

     use KurtJensen\MyPlugin\Traits\MyTrait;

I get:

    Trait 'KurtJensen\MyPlugin\Components\KurtJensen\MyPlugin\Traits\MyTrait' not found

Last updated

Scott
Scott

It looks like you just forgot to add the leading backslash. Give this a try...

use \KurtJensen\MyPlugin\Traits\MyTrait;

Last updated

KurtJensen
KurtJensen

Thank you Scott. That did it.

Last updated

Scott
Scott

Excellent, glad I could help!

Last updated

ichakesachin3010215
ichakesachin3010215

Yes. How to call traits function from component (defualt.htm) page

zakir2229
zakir2229

+1 fo r scott solution.

1-6 of 6

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