walkor / webman

Probably the fastest PHP web framework in the world.
https://webman.workerman.net/
MIT License
2.18k stars 214 forks source link

How To Add Custom Helper / Library in App #32

Open haidarvm opened 4 years ago

haidarvm commented 4 years ago

Just Like Codeigniter or any other php framework, they have Helper / Library for custom function & library

function allowTags($text) {
    $tags = '<strong><h1><h2><h3><h4><br><hr><b><ol><ul><u><span><li><a><img><iframe>';
    return strip_tags(html_entity_decode(trim($text)), $tags);
}

for example I create multiple Controller and want to call the function allowTags() in any controller

haidarvm commented 4 years ago

@walkor , @passwalls , please I really need this feature. I would definitely donate this project soon

walkor commented 4 years ago

Webman currently does not strictly specify the location of custom function & library.

You can put your helper functions into for example app/functions.php. And add line "./app/functions.php" in composer.json like this.

"autoload": {
    "files": [
      "./support/helpers.php",
      "./app/functions.php"
    ]
  },

Run command composer dump-autoload then the ./app/functions.php will be loaded automatically .

Make your class file conform to psr4 rules, and it will be loaded automatically. For example app/common/User.php.

namespace app\common;
class User
{
}

It will be loaded automatically when it used.

haidarvm commented 4 years ago

Oh, Ok i got it. For temporary I will using composer psr4