2011-08-25

Getting Neo4jPHP working with CodeIgniter 2

This post is based heavily off of Integrating Doctrine 2 with CodeIgniter 2. I haven't tested it myself, so please use it as a starting point and let me know if I should update anything.

Here are the steps to get Neo4jPHP set up as a CodeIgniter 2 library:
  1. Copy the 'Everyman' folder to application/libraries
  2. Create a file called Everyman.php in application/libraries
  3. Copy the following code into Everyman.php:
    <?php
    class Everyman
    {
        public function __construct()
        {
            spl_autoload_register(array($this,'autoload'));
        }
    
        public function autoload($sClass)
        {
            $sLibPath = __DIR__.DIRECTORY_SEPARATOR;
            $sClassFile = str_replace('\\',DIRECTORY_SEPARATOR,$sClass).'.php';
            $sClassPath = $sLibPath.$sClassFile;
            if (file_exists($sClassPath)) {
                require($sClassPath);
            }
        }
    }
    
  4. Add the following line to application/config/autoload.php:
    $autoload['libraries'] = array('everyman');
    

This is actually a generic autoloader that will attempt to autoload any namespaced class under application/library where the class name matches the file path.

18 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. AND it WORKS!

    All it needed was the <?PHP tag in the beginning... obvious but just copied code from above.

    Should be:

    <?PHP
    class Everyman
    {
    public function __construct()
    {
    spl_autoload_register(array($this,'autoload'));
    }

    public function autoload($sClass)
    {
    $sLibPath = __DIR__.DIRECTORY_SEPARATOR;
    $sClassFile = str_replace('\\',DIRECTORY_SEPARATOR,$sClass).'.php';
    $sClassPath = $sLibPath.$sClassFile;
    if (file_exists($sClassPath)) {
    require($sClassPath);
    }
    }
    }

    ReplyDelete
  3. I'm really glad to hear that it worked for you. I fixed the code to include the opening <?php tag.

    Good luck! I'd love to see how you're using Neo4j in your projects.

    ReplyDelete
  4. I will keep you in the loop Josh.

    Thank you again!

    ReplyDelete
  5. Done this and it loads fine. From reading your examples on Github, how would I do it with the $this->everyman way of CodeIgniter?

    ReplyDelete
  6. I'm not to familiar with CI, but based off the Doctrine example in the linked blog post, you could try adding:

        $this->neo4j = new Everyman\Neo4j\Client();

    to the end of the __construct() method. Then in your code, you would access it as:

        $this->everyman->neo4j

    which would give you the Client instance. Let me know if that works.

    ReplyDelete
  7. Yep, that works. It's a little long winded IMO, usually CodeIgniter libraries have the functions available directly from the Library name itself. I'm completely new to the namespace stuff, and fairly new to OOP.

    With this working, do you think it could be made to have $this->everyman be the Client instance?

    Thanks for your help with this, really looking forward to using this and neo4j in general!

    ReplyDelete
  8. Tecnically, Everyman is a vendor namespace, so it is conceivable that other components besides Neo4jPHP could be release under that namespace.  In the Doctrine example, the entity manager is referenced as $this->doctrine->em for the same reason.If you think it is too long to type out every time, you could bind the client to a local variable where ever you need it:

        $neo4j = $this->everyman->neo4j;
        $neo4j->doStuff(...);

    You could also define your own methods on the Everyman object.  Or, you could have the Everyman object extend Everyman\Neo4j\Client, overwrite the constructor with whatever autoloading and custom config you want (make sure to call parent::__construct at the end), and then $this->everyman would be an instance of the client that you couls use directly.

    ReplyDelete
  9. Thanks for your advice, I've bound it to $this->neo4j in the constructor of the Controller, I was just wondering if it could have been done at the namespace level.

    Cheers!

    ReplyDelete
  10. followed exact same steps mentioned above but got the following error

    Fatal error: Class 'Everyman\Neo4j\Transport' not found ....

    ReplyDelete
  11. hai josh, could you give an example how to use in the controller, i've try to follow your instruction and then create the object but always get an error. this is my script in controller

    function index(){
        $this->everyman->autoload("Transport");
        $transport = new Transport();
    }
    please help me, master josh...

    thanks
    :)

    ReplyDelete
  12. What error are you getting?

    Be careful of namespacing issues. If you don't have a `use` line that references Everyman\Neo4j\Transport at the top of your script, you have to use that full class name when creating the object, not just "Transport".

    In current versions of Neo4jPHP, you don't need to create a Transport object directly. You can just create an Everyman\Neo4j\Client object and it will bind to localhost by default. Also, you don't need to call "autoload"; that happens for you when you add the `$autoload['libraries'] = array('everyman');`.

    I don't use CodeIgniter, so I can't think of what else to tell you to try unless you post your error message.

    ReplyDelete
  13. Making it so clear and exceptional guidance about Neo4jPHP working with CodeIgniter 2 .There are certainly a lot of details like that to take into consideration. That is a great point to bring up.

    ReplyDelete
  14. Nice post, keep up with this interesting work. It really is good to know about this topic is being covered also on this web site so thanks for taking time to discuss this about Neo4jPHP set up as a CodeIgniter library!

    T

    ReplyDelete
  15. I tried the following

    1.added the everyman folder to the libraries folder

    2. Added the file Everyman.php to the libraries folder

    3. Added the line in the autoload.php file.

    After that inside a model i tried the following

    echo "Library Initalized";
    $transport=new Transport('localhost', 7474);
    echo "Transport Initialized";
    $client = new Client($transport);
    echo "Client initialized";
    $keanu = new Node($client);
    echo "Node Initalized";


    The code stops executing after Library Initalized. Most probably its not able to find the Transport class!
    what am i missing?

    ReplyDelete
  16. Is it a namespacing issue? What if you try "Everyman\Neo4j\Transport" ?

    ReplyDelete
  17. same issue even when i tried to use $transport=new Everyman\Neo4j\Transport('localhost', 7474);

    ReplyDelete
  18. Gambling in Indiana - JTAHub
    Find a casino at 오산 출장안마 your local location today to find out more about gambling in Indiana. Our experts provide 안산 출장마사지 the best casino bonus 성남 출장마사지 codes to online Nov 평택 출장안마 2, 2021 · Uploaded by Gambling in Indiana 울산광역 출장안마

    ReplyDelete