2

I am trying to add a custom class to my Yii2 application, so I created a folder called "Payment" and inside this folder I created a class called Test.php, and the namespace is "payment"

 <?php
     namespace payment;
     class Test{
          public static function test(){
            echo "Hello";
          }
      }

in my controller I add this

use payment;

and I am trying to access the method in the test class but I got class not found fatal error. what is wrong? Structure: + project-name-folder: -api -Payment -Test.php -modules - v1 - controllers - HelloController.php

9
  • Basic or advanced template? Commented Jan 22, 2017 at 9:38
  • advanced, the classes I am trying to inside API module Commented Jan 22, 2017 at 9:53
  • It's most likely because you're using incorrect path or defined wrong namespace path. Can you give me exactly where did you create that payment folder? Commented Jan 22, 2017 at 9:55
  • projectname -> api -> Payment Commented Jan 22, 2017 at 9:59
  • That api is in frontend, backend or common folder? Or none of these? If none, you may need to put inside of these then first. Commented Jan 22, 2017 at 10:00

1 Answer 1

3

If this is basic project template all classes should be under root namespace app.
If this is advanced project template classes should be under root namespace based on the application they are in so frontend, common, or backend.

Your class namespace should be changed like:

namespace <root>\payment;

where <root> is app, frontend, common, or backend. Every use of this class should be modified accordingly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.