skip to Main Content

I want to create an EBay API wrapper object. In Rails, I would create a helper so it would be available in the controller and view. Where do I create such an object in Yii? There is no helpers/ directory. This was the closest concept I could find: http://www.yiiframework.com/doc-2.0/guide-structure-application-components.html

2

Answers


  1. Chosen as BEST ANSWER

    I managed to get this to work with the following, but I think there is a better answer.

    helpers/myhelpers.php
    <?php
    namespace apphelpers;
    
    function isGrid() {
      return true;
    }
    ?>
    
    views/shopping/index.php
    <?php
    use apphelpers; // this doesn't seem to work - how does Yii know to include the files?
    include('../helpers/myhelpers.php');
    ...
    <?= apphelpersisGrid() ?>
    

    For some reason, you have to still give the full namespace path to the function. It won't work without it.


  2. You can always create a helpers directory if you want, there is nothing to stop you, just create your namespace accordingly. I personally put them in the components directory.

    You are using namespaces now 🙂 enjoy it. If you are using the advance app then there is already defined a namespace for the backend for example that is called….. backend. What does this mean it means that when you use a class that is found in a known namespace it starts from there and it tries to guess the path to your class.

    For example:

    use backend/helpers/foo/Bar;
    

    Bar should be found in the backend/helpers/foo directory. Yii2 will know where to find it because it knows where backend is.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search