Here are the steps to get Neo4jPHP set up as a CodeIgniter 2 library:
- Copy the 'Everyman' folder to application/libraries
- Create a file called Everyman.php in application/libraries
- 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); } } }
- 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.
This comment has been removed by the author.
ReplyDeleteAND it WORKS!
ReplyDeleteAll 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);
}
}
}
I'm really glad to hear that it worked for you. I fixed the code to include the opening <?php tag.
ReplyDeleteGood luck! I'd love to see how you're using Neo4j in your projects.
I will keep you in the loop Josh.
ReplyDeleteThank you again!
Done this and it loads fine. From reading your examples on Github, how would I do it with the $this->everyman way of CodeIgniter?
ReplyDeleteI'm not to familiar with CI, but based off the Doctrine example in the linked blog post, you could try adding:
ReplyDelete$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.
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.
ReplyDeleteWith 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!
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:
ReplyDelete$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.
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.
ReplyDeleteCheers!
followed exact same steps mentioned above but got the following error
ReplyDeleteFatal error: Class 'Everyman\Neo4j\Transport' not found ....
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
ReplyDeletefunction index(){
$this->everyman->autoload("Transport");
$transport = new Transport();
}
please help me, master josh...
thanks
:)
What error are you getting?
ReplyDeleteBe 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.
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.
ReplyDeleteNice 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!
ReplyDeleteT
I tried the following
ReplyDelete1.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?
Is it a namespacing issue? What if you try "Everyman\Neo4j\Transport" ?
ReplyDeletesame issue even when i tried to use $transport=new Everyman\Neo4j\Transport('localhost', 7474);
ReplyDeleteGambling in Indiana - JTAHub
ReplyDeleteFind 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 울산광역 출장안마