16

I am creating an application in CakePHP 3.0, in this application I want to draw SVG graphs of data using a php class that I have written. What would be the proper way to go about using this class in my CakePHP 3 project?

More specifically:

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

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

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

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Jon Lachmann
  • 381
  • 1
  • 3
  • 10
  • 1
    Hey Jon, welcome to Stack Overflow! This question will probably be close because you don't have a specific programming question, but I'd like to point you in the right direction. First, Cake 3.0 uses [PSR-4](http://www.php-fig.org/psr/psr-4/) autoloading so that's a good place to start. Also, you read up on [Cake's MVC conventions and layers](http://book.cakephp.org/3.0/en/intro.html) to decide where your class might belong. Or [create your own](http://culttt.com/2014/05/07/create-psr-4-php-package/) PSR-4 package and load it somewhere within Cake (in the appropriate layer is probably best). – jeremyharris Feb 14 '15 at 19:19
  • Does this answer your question? [Where to place a custom PHP class in CakePHP 3?](https://stackoverflow.com/questions/34147426/where-to-place-a-custom-php-class-in-cakephp-3) – monsur.hoq Jul 03 '22 at 13:19

1 Answers1

29

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;
     }
    
hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
Fazal Mohammad
  • 426
  • 4
  • 6
  • This was a really great answer! It solved my problem completely, and left nothing to wonder about. This should be added to the CakePHP manual in some way, would it be ok if i took parts of it and made a pull request on the CakePHP 3.0 manual? Anyway, thank you! =) – Jon Lachmann Feb 15 '15 at 16:43
  • i am using this method and its working fine but i have one more problem actually i have one class as Myclass and its extends the second class in core php i have just include the file and than extend the class because that is abstract but i dont know how to use it in cakephp – Deepak Goyal Nov 19 '15 at 06:46
  • Great answer, they should add that indeed. – Isengo Jun 30 '16 at 07:43
  • I see Vendors as 3rd party that are not included in the actual source of the App. This way you can deploy your code using Composer. If you want to maintain your 3rd party code and have it maintained by Composer I think this is a great solution. Otherwise I would suggest incorporating small classes into Cake Components. This is a much more simple way to attach custom classes or code for use with your Controller. https://book.cakephp.org/3.0/en/controllers/components.html#creating-a-component – Dooltaz Oct 06 '17 at 16:40
  • 1
    This method will have issues in deployment or git pushes as vendor is ignored? isn't it? – Somnath Muluk Nov 05 '18 at 06:25