Ckappo
Ckappo

Reputation: 597

CakePHP3 best way to include own php Classes

I search for that Question a lot and find no really satisfied answer. So I want to use cakePHP3 for web-app development. My old Apps I write without a framework, and have a folder structure like this:

/config
/views
/classes
...

Classes I include with require_once and everthink work fine.

Know. How or where I should put my PHP Classes in Cake structure? Should I put my Classes into the Vendor folder and get it with require_once(ROOT .DS. "vendor" . DS . "CLASS" . DS . "myClass.php");

What is the best way?

Upvotes: 0

Views: 518

Answers (1)

Kamran
Kamran

Reputation: 2741

Cake uses composer (psr-4) to autoload classes so there is no need to do require. You can make a folder under src directory.Lets say your folder name is CustomClasses. Make a new class named TestClass.php in this folder.

namespace App\CustomClasses;
class TestClass
{
    public function test()
    {
        echo "Ok";
    }
}`

From your controller method call your custom class with namespace.

 $class = new \App\CustomClasses\TestClass();
 echo $class->test();

Upvotes: 1

Related Questions