How to Use My Own External Class in Cakephp 3.0

How can I use my own external class in CakePHP 3.0?

What are the naming conventions? Do I need to use a specific namespace?

Your SVG graphs class should have a namespaces. For namespaces you can see http://php.net/manual/en/language.namespaces.rationale.php

Where do I put the file that contains the PHP class?

  1. Create a folder by author(here might be your name, as you are the author) in vendor

  2. Then create your class inside of it
    convention is vendor/$author/$package . You can read more http://book.cakephp.org/3.0/en/core-libraries/app.html#loading-vendor-files

How can I include it and use it in a controller or a view?

a) To include:

require_once(ROOT .DS. 'Vendor' . DS . 'MyClass' . DS . 'MyClass.php');

(replace MyClass by your foldername and MyClass.php by your filename.php)

b) To use it:

add use MyClass\MyClass; in your controller

For example I want to add MyClass in a controller. Steps that worked for me

  1. Creating vendor\MyClass folder
  2. Pasting MyClass.php in that folder
  3. adding namespace MyClass; at the top of MyClass.php

MyClass.php have following code for example:

namespace MyClass;

class MyClass
{
public $prop1 = "I'm a class property!";

public function setProperty($newval)
{
$this->prop1 = $newval;
}

public function getProperty()
{
return $this->prop1 . "<br />";
}
}

  1. Adding use MyClass\MyClass; at the top of controller

  2. Then including it in my controller action. My action sample

       public function test()
    {
    require_once(ROOT .DS. "Vendor" . DS . "MyClass" . DS . "MyClass.php");

    $obj = new MyClass;
    $obj2 = new MyClass;

    echo $obj->getProperty();
    echo $obj2->getProperty();
    exit;
    }

Where to place a custom PHP class in CakePHP 3?

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

Loading custom class in CakePHP3

I changed the name from Library to Berry (My first name).

Apparently you can't call it Library. Probably used somewhere else in Cake.

Use Zip Class in CakePHP 3.x

You forgot the leading backslash:

$zip = new \ZipArchive();

Namespaces are not too different from file system paths. If you have this:

mkdir /foo.txt
cd /home/jim

... you can't do this:

type foo.txt

You need:

type /foo.txt

How to include a class from outside of cakephp folder

You need to ensure PHP can find the class in the correct namespace using new \Push() (note the backslash before the class name):-

require_once( ROOT . DS . '..' . DS . 'RowPHP'. DS . 'push.php');
$pushOb = new \Push();

CakePHP 3.0 vendor class not found

CakePHP 3.0 uses namespaces. So use proper namespace for your vendor class or if it's not using namespaces prefix the class name with backslash when using it.

E.g. $object = new \Test_Class();.

Cakephp 3 - How to integrate external sources in table?

The most simple solution would be to use a result formatter to fetch and inject the external data.

The more sophisticated solution would a custom association, and a custom association loader, but given how database-centric associations are, you'd probably also have to come up with a table and possibly a query implementation that handles your LDAP datasource. While it would be rather simple to move this into a custom association, containing the association will look up a matching table, cause the schema to be inspected, etc.

So I'll stick with providing an example for the first option. A result formatter would be pretty simple, something like this:

$this->Articles
->find()
->formatResults(function (\Cake\Collection\CollectionInterface $results) {
$userIds = array_unique($results->extract('user_id')->toArray());

$users = LDAPConnector::getUsers($userIds);
$usersMap = collection($users)->indexBy('id')->toArray();

return $results
->map(function ($article) use ($usersMap) {
if (isset($usersMap[$article['user_id']])) {
$article['user'] = $usersMap[$article['user_id']];
}

return $article;
});
});

The example makes the assumption that the data returned from LDAPConnector::getUsers() is a collection of associative arrays, with an id key that matches the user id. You'd have to adapt this accordingly, depending on what exactly LDAPConnector::getUsers() returns.

That aside, the example should be rather self-explanatory, first obtain a unique list of users IDs found in the queried articles, obtain the LDAP users using those IDs, then inject the users into the articles.

If you wanted to have entities in your results, then create entities from the user data, for example like this:

$userData = $usersMap[$article['user_id']];
$article['user'] = new \App\Model\Entity\User($userData);

For better reusability, put the formatter in a custom finder. In your ArticlesTable class:

public function findWithUsers(\Cake\ORM\Query $query, array $options)
{
return $query->formatResults(/* ... */);
}

Then you can just do $this->Articles->find('withUsers'), just as simple as containing.

See also

  • Cookbook > Database Access & ORM > Query Builder > Adding Calculated Fields
  • Cookbook > Database Access & ORM > Retrieving Data & Results Sets > Custom Finder Methods


Related Topics



Leave a reply



Submit