12

I have a new PHP class which I would like to call from a Controller. Where, in the CakePHP folder structure, should I place this new class and what is the procedure to invoke or make use of it from a controller? Thanks in advance for your cooperation!

Jorge
  • 664
  • 1
  • 12
  • 33

3 Answers3

28

From my point of view, you can reuse any own class and also any third parties class as a utility class. If so, then you can place the class into src/Utility folder. Please use the proper namespace. After that, you can use that class anywhere in CakPHP 3.x.

HOW TO PLACE:

Say, you have a class named Jorge, save it into src/Utility folder using file name Jorge.php. Put namespace App\Utility; statement top of your Jorge.php file.

HOW TO USE:

In the file where you want to use this class, just put use App\Utility\Jorge;. After that, you can call the class into that file.

ALTERNATIVE SOLUTION:

If you have a third party package of many classes, then you can follow https://stackoverflow.com/a/28527683/1787600

monsur.hoq
  • 1,135
  • 16
  • 25
1

Most CakePHP derived code that you'd create will fall under the structure outlined by the documentation.

http://book.cakephp.org/3.0/en/intro/cakephp-folder-structure.html

For "totally arbitrary utility classes" as I'm guessing your meaning, CakePHP treats those as "third-party dependencies" (conceptually) and they recommend putting them in the "vendors" folder. You can ignore their recommendation to not edit things in that folder when the new content is your own utility project.

For loading, use good old php's require family of functions.

http://book.cakephp.org/3.0/en/core-libraries/app.html

If you happen to not be using Composer in your application, you will need to manually load all vendor libraries yourself.

You can make use of CakePHP global constants/functions to build the paths needed for require.

http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html

APP, APP_DIR, WWW_ROOT, etc.

starlocke
  • 3,407
  • 2
  • 25
  • 38
  • 5
    "*For loading, use good old php's require family of functions.*" - CakePHP is using namespaces and the PSR autoloader. There is almost no need, except when using ancient code, to use require anymore. Just make sure the namespace and path match and it will find the class automatically when used. – floriank Dec 08 '15 at 08:01
  • Sure, assuming he'll author up a shiny modern file he can go used autoloading. However, if he's not using a full stack of modernized utilities, maybe it will be easier to use an old school `include`, like when pulling in some old-school third-party library? What if he's making sure to write a function instead of a class? OP's question was class focused, but, the more general case is "I will want to load anything from a file, maybe even some data / images / sound / video using a well-made, old set of libraries that doesn't fit with PSR autoload". – starlocke Dec 08 '15 at 12:09
1

You can put it anywhere you want in /src/ folder. You can collect your custom classes in folders like: Interface, DTO, Utility, Custom or whatever. Since cakePHP 3.x uses PSR-4 autoloader the class will be instantiated once used. You just need to remember to add correct namespace. For example If you want to use custom interface add file src/Custom/Owned.php

<?php
namespace App\Custom;
interface Owned
{
    /**
     * Find username owning object by object primary id
     * @param $id
     * @return string|int
     */
    public function owner($id) : string;
}
...

Then you can use it right away:

...
class UsersTable extends Table implements Owned
{
...

If you going to reuse your classes better solution would be to create your own packagist package and include it by composer so it will be stored in "/vendor"

Amorphous
  • 779
  • 7
  • 27