本文介绍了在Symfony2中设置NEO4j的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我是Symfony的新手,已经开始阅读和嘲笑了。我可以安装Symfony2和一个NEO4j数据库。我也可以将引导包含在我的Symfony项目中,并显示一个基本的主页。 现在我想将我的项目连接到数据库。我想从基本功能开始,如注册和用户系统。 我正在使用以下(通过作曲家安装): perman / neo4jphp:dev-master,hirevoice / neo4jphp-ogm:dev-master, klaussilveira / neo4j-ogm-bundle:dev-master 这些应该给我我的功能需要。我在src下创建了一个UserBundle,具有以下文件夹结构: src | -Controller | -DependencyInjection | -Entity | -Form | -Ressources | -UserBundle.php 我现在想在 http://symfony.com/doc/current/cookbook/doctrine/registration_form.html ,而不是使用他们的数据库,我想使用Neo4j。有人可以指出我应该如何操作教程的代码,以便能够将用户注册到neo4j图表中?我已经尝试了自己,但是文档是neo4j-ogm的粗体,我只是错误地产生错误。我现在已经删除了Bundle,并重新创建了它,以重新开始。 如果他们的步骤被认为是普遍的知识,那么我被特别整合,因此被忽略了? 我现在已经能够设置所有内容,以便显示注册表单。但是当提交时,显示以下错误: 无法提交批处理[401]:标题:数组( [Date] => Sun,05 Jul 2015 13:08:20 GMT [Content-Type] => application / json; charset = UTF-8 [ WWW-Authenticate] => None [Content-Length] => 139 [Server] => Jetty(9.2.4.v20141103))正文: Array ( [errors] => Array ( [0] => Array ( [message] => [code] => Neo.ClientError.Security.AuthorizationFailed ) ) ) 500内部服务器错误 - 异常 我假设我的连接身份验证不工作。在我的config.yml我已经定义了以下连接: #Neo4j config neo4j_ogm: transport: 'curl'主机:'localhost'端口:7474 用户名:'foo'密码:'bar' debug:true 但这似乎不起作用。还有哪里我必须设置连接详细信息? 更新 谢谢Christophe Willemsen为您的答案。 出现错误是因为认证无效。通过导航到Neo4j安装文件夹并编辑neo4j-server.properties文件以允许所有访问,我现在可以将简单的数据保存到图形中。我没有尝试执行任何检查或密码加密,但是基本的创建一个具有属性的节点现在可以工作。 我现在想完成的是: 让它成为可能密码保护Neo4j连接,并将存储在config.yml文件中的连接信息 创建并执行各种检查(电子邮件地址已在DB / 中密码强度足够...) 创建和实施密码加密 创建从neo4j数据库中获取数据的成员系统 如果您有任何提示可以获取有关此主题的更多信息,或者如果您已经做了类似的工作,帮助将不胜感激。 / p> 将文件添加到我的帖子下面,以防这些对其他人有用。请注意:我刚刚开始学习Symfony和Neo4j,代码可能包含错误或主要的漏洞。如果我有任何运气实现进一步的neo4j相关事情,我也会尝试更新这篇文章。 在文件neo4j-server.properties(位于/ etc / neo4j在我的安装)编辑以下行: #让网络服务器只侦听指定的IP。默认是localhost(只有#接受本地连接)。取消注释允许任何连接。在修改之前,请参阅neo4j手册中的#security部分。 org.neo4j.server.webserver.address = 0.0.0.0 #需要(或禁用)auth访问Neo4j 的要求dbms.security.auth_enabled = false 在我的UserBundle中,以下文件可能与其他文件完全相同: / src / UserBundle / Controller / AccountController.php // src / Acme / AccountBundle / Controller / AccountController.php 命名空间UserBundle\Controller; 使用Symfony\Bundle\FrameworkBundle\Controller\Controller; 使用UserBundle\Form\Type\RegistrationType; 使用UserBundle\Form\Model\Registration; 使用Symfony\Component\HttpFoundation\Request; //使用HireVoice\Neo4j\EntityManager作为EntityManager; class AccountController extends Controller { public function registerAction() { $ registration = new Registration(); $ form = $ this-> createForm(new RegistrationType(),$ registration,array('action'=> $ this-> generateUrl('account_create'), )); return $ this-> render('UserBundle:Account:register.html.twig', array('form'=> $ form-> createView ())); } public function createAction(Request $ request) { $ em = $ this-> container->得到( 'neo4j.manager'); // $ repo = $ em-> getRepository('Entity\\\User'); $ form = $ this-> createForm(new RegistrationType(),new Registration()); $ form-> handleRequest($ request); if($ form-> isValid()){ $ registration = $ form-> getData(); $ em-> persist($ registration-> getUser()); $ em-> flush(); return $ this-> redirectToRoute('account_success'); // return $ this-> redirect('http://4740.hostserv.eu/twohive/web/app_dev.php/register/success'); } return $ this-> render('UserBundle:Account:register.html.twig', array('form' => $ form-> createView())); } public function successAction() { return $ this-> render('UserBundle:Account:success.html.twig'); } } / src / UserBundle / Entity / User.php <?php // src / UserBundle / Entity / User。 php 命名空间UserBundle\Entity; 使用HireVoice\Neo4j\Annotation作为OGM; / ** * @ OGM\Entity * / 类用户 { / ** * @ OGM\Auto * / protected $ id; / ** * @ OGM\Property * @ OGM\Index * / protected $ email; / ** * @ OGM\Property * / protected $ plainPassword; public function getId() { return $ this-> id; } public function getEmail() { return $ this-> email; } public function setEmail($ email) { $ this-> email = $ email; } public function getPlainPassword() { return $ this-> plainPassword; } public function setPlainPassword($ password) { $ this-> plainPassword = $ password; } } / src / UserBundle / Form /模型/ Registration.php(如Symfony-Book中所述) <?php // src / Acme / AccountBundle / Form / Model / Registration.php namespace UserBundle\Form\Model; 使用Symfony\Component\Validator\Constraints作为Assert; 使用UserBundle\Entity\User; class注册 { / ** * @ Assert\Type(type =UserBundle\Entity\User) * @ Assert\Valid() * / protected $ user; / ** * @ Assert\NotBlank() * @ Assert\True() * / protected $ termsAccepted; public function setUser(User $ user) { $ this-> user = $ user; } public function getUser() { return $ this-> user; } public function getTermsAccepted() { return $ this-> termsAccepted; } public function setTermsAccepted($ termsAccepted) { $ this-> termsAccepted =(bool)$ termsAccepted; } } / src / UserBundle / Form / Type / RegistrationType.php(如Symfony-Book所述) // UserBundle / Form / Type / RegistrationType.php namespace UserBundle\Form\Type; 使用Symfony\Component\Form\AbstractType; 使用Symfony\Component\Form\FormBuilderInterface; class RegistrationType extends AbstractType { public function buildForm(FormBuilderInterface $ builder,array $ options) { $ builder-> add(' user',new UserType()); $ builder-> add('terms','checkbox', array('property_path'=>'termsAccepted')); $ builder-> add('Register','submit'); } public function getName() { return'registration'; } } / src / UserBundle / Form /键入/ UserType.php(如Symfony-Book中所述) <?php // UserBundle / Form / Type / UserType.php namespace UserBundle\Form\Type; 使用Symfony\Component\Form\AbstractType; 使用Symfony\Component\Form\FormBuilderInterface; 使用Symfony\Component\OptionsResolver\OptionsResolver; class UserType extends AbstractType { public function buildForm(FormBuilderInterface $ builder,array $ options) { $ builder-> add('电子邮件,电子邮件); $ builder-> add('plainPassword','repeated',array('first_name'=>'password','second_name'=& b $ b'type'=>'password',)); } public function configureOptions(OptionsResolver $ resolver) { $ resolver-> setDefaults(array('data_class'=> UserBundle\Entity\User')); } public function getName() { return'user'; } } / src / UserBundle / Resources / config / routing.yml account_register: path:/ register defaults: {_controller:UserBundle:Account:register} account_create:路径:/ register / create 默认值:{_controller:UserBundle:Account:create} account_success:路径:/ register / success 默认值:{_controller:UserBundle:Account:success} 解决方案我之前(〜两年)做了这个实现,但没有更新,因为我不再使用这些供应商了。 / p> 这是分为两个组合,这可能会给你一些想法和例子: https://github.com/ikwattro/KwattroNeo4jOGMBundle https://github.com/ikwattro/Neo4jUserBundle 基本步骤是常见的,很多信息可以在Symfony文档中找到。 根据neo4jphp-ogm注释创建您的用户实体 创建用户提供商 - >安全性 创建一个用户管理器,当认证发生时, / p> 关于认证的最后一个错误,通过查看 klaussilvera / neo4j-ogm-捆绑最后提交日期,机会很高,它不支持在2.2中添加neo4j身份验证。 您可能需要禁用neo4j身份验证对于您的第一个测试或分支当前的存储库,并创建一个PR,在组件的ConfigDefinition中添加认证参数。 I am new to Symfony and have started reading and toying about. I was able to install Symfony2 and a NEO4j database. I was also able to include bootstrap into my Symfony project and display a basic homepage.I would now like to connect my project to the database. I would like to start with basic functionallity such as registration and user system.I am currently using following (installed via composer): "everyman/neo4jphp": "dev-master", "hirevoice/neo4jphp-ogm": "dev-master", "klaussilveira/neo4j-ogm-bundle": "dev-master"These should give me the functionality I need. I have then created a UserBundle under src with following folder structure:src |-Controller |-DependencyInjection|-Entity|-Form|-Ressources|-UserBundle.phpI would now like to go through the tutorial at http://symfony.com/doc/current/cookbook/doctrine/registration_form.html but instead of using their database, I would like to use Neo4j. Could someone point out where and how I should manipulate the code of the tutorial to be able to register a user into the neo4j graph? I have tried myself but the documentation is scarse of the neo4j-ogm and I have only produced errors upon errors. I have now deleted the Bundle and created it again to start anew.I am especially interessted if their are steps that are assumed to be common knowledge and are thus left out?I have now been able to set up everything so that the registration form is shown. But when submitting, following error is shown:Unable to commit batch [401]:Headers: Array([Date] => Sun, 05 Jul 2015 13:08:20 GMT[Content-Type] => application/json; charset=UTF-8[WWW-Authenticate] => None[Content-Length] => 139[Server] => Jetty(9.2.4.v20141103))Body: Array([errors] => Array([0] => Array([message] => No authorization header supplied.[code] => Neo.ClientError.Security.AuthorizationFailed)))500 Internal Server Error - Exception I am assuming that my connection authentication is not working. In my config.yml I have defined following connection:# Neo4j config neo4j_ogm: transport: 'curl' host: 'localhost' port: 7474 username: 'foo' password: 'bar' debug: trueBut this does not seem to work. Where else do I have to set up the connection details?UpdateThank you Christophe Willemsen for your answer.The error was produced because the authentication did not work. By navigating to the Neo4j installation folder and editing the "neo4j-server.properties" file to allow all access, I am now able to save simple data to the graph. I have not tried to implement any checks or passwort encryptions, but the very basic "create a node with properties" now works. What I would like to accomplish now would be:Make it possible to password protect the Neo4j connection and havethe connection information stored in a config.yml fileCreate and implement various checks (is email address already in DB /Is password strong enough...)Create and implement password encryptionCreate member-system which gets data out of the neo4j databaseIf you have any tips on where I can get more information on this topic or if you have already done something similar, help would be appreciated.Have added the files to my post below in case these are useful for someone else. Be aware: I have only just begun learning Symfony and Neo4j and the code could contain errors or major vulnerabilities. I will also try to update this post if I have any luck implementing further neo4j related things.In the file "neo4j-server.properties" (located under "/etc/neo4j" on my install) edit following lines as such:# Let the webserver only listen on the specified IP. Default is localhost (only# accept local connections). Uncomment to allow any connection. Please see the# security section in the neo4j manual before modifying this.org.neo4j.server.webserver.address=0.0.0.0# Require (or disable the requirement of) auth to access Neo4jdbms.security.auth_enabled=falseIn my UserBundle, following files could be of interesst to others:"/src/UserBundle/Controller/AccountController.php"<?php// src/Acme/AccountBundle/Controller/AccountController.phpnamespace UserBundle\Controller;use Symfony\Bundle\FrameworkBundle\Controller\Controller;use UserBundle\Form\Type\RegistrationType;use UserBundle\Form\Model\Registration;use Symfony\Component\HttpFoundation\Request;// use HireVoice\Neo4j\EntityManager as EntityManager;class AccountController extends Controller{ public function registerAction() { $registration = new Registration(); $form = $this->createForm(new RegistrationType(), $registration, array( 'action' => $this->generateUrl('account_create'), )); return $this->render( 'UserBundle:Account:register.html.twig', array('form' => $form->createView()) ); } public function createAction(Request $request) { $em = $this->container->get('neo4j.manager'); //$repo = $em->getRepository('Entity\\User'); $form = $this->createForm(new RegistrationType(), new Registration()); $form->handleRequest($request); if ($form->isValid()) { $registration = $form->getData(); $em->persist($registration->getUser()); $em->flush(); return $this->redirectToRoute('account_success'); // return $this->redirect('http://4740.hostserv.eu/twohive/web/app_dev.php/register/success'); } return $this->render( 'UserBundle:Account:register.html.twig', array('form' => $form->createView()) ); } public function successAction() { return $this->render('UserBundle:Account:success.html.twig'); }}"/src/UserBundle/Entity/User.php"<?php// src/UserBundle/Entity/User.phpnamespace UserBundle\Entity;use HireVoice\Neo4j\Annotation as OGM;/** * @OGM\Entity */class User{ /** * @OGM\Auto */ protected $id; /** * @OGM\Property * @OGM\Index */ protected $email; /** * @OGM\Property */ protected $plainPassword; public function getId() { return $this->id; } public function getEmail() { return $this->email; } public function setEmail($email) { $this->email = $email; } public function getPlainPassword() { return $this->plainPassword; } public function setPlainPassword($password) { $this->plainPassword = $password; }}"/src/UserBundle/Form/Model/Registration.php" (As described in the Symfony-Book)<?php// src/Acme/AccountBundle/Form/Model/Registration.phpnamespace UserBundle\Form\Model;use Symfony\Component\Validator\Constraints as Assert;use UserBundle\Entity\User;class Registration{ /** * @Assert\Type(type="UserBundle\Entity\User") * @Assert\Valid() */ protected $user; /** * @Assert\NotBlank() * @Assert\True() */ protected $termsAccepted; public function setUser(User $user) { $this->user = $user; } public function getUser() { return $this->user; } public function getTermsAccepted() { return $this->termsAccepted; } public function setTermsAccepted($termsAccepted) { $this->termsAccepted = (bool) $termsAccepted; }}"/src/UserBundle/Form/Type/RegistrationType.php" (As described in the Symfony-Book)<?php// UserBundle/Form/Type/RegistrationType.phpnamespace UserBundle\Form\Type;use Symfony\Component\Form\AbstractType;use Symfony\Component\Form\FormBuilderInterface;class RegistrationType extends AbstractType{ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('user', new UserType()); $builder->add( 'terms', 'checkbox', array('property_path' => 'termsAccepted') ); $builder->add('Register', 'submit'); } public function getName() { return 'registration'; }}"/src/UserBundle/Form/Type/UserType.php" (As described in the Symfony-Book)<?php// UserBundle/Form/Type/UserType.phpnamespace UserBundle\Form\Type;use Symfony\Component\Form\AbstractType;use Symfony\Component\Form\FormBuilderInterface;use Symfony\Component\OptionsResolver\OptionsResolver;class UserType extends AbstractType{ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('email', 'email'); $builder->add('plainPassword', 'repeated', array( 'first_name' => 'password', 'second_name' => 'confirm', 'type' => 'password', )); } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'UserBundle\Entity\User' )); } public function getName() { return 'user'; }}"/src/UserBundle/Resources/config/routing.yml"account_register: path: /register defaults: { _controller: UserBundle:Account:register }account_create: path: /register/create defaults: { _controller: UserBundle:Account:create }account_success: path: /register/success defaults: { _controller: UserBundle:Account:success } 解决方案 I did this implementation some time ago (~ two years) but didn't updated it due to the fact I don't use these vendors anymore.This was splitted in two bundles, and that might give you some ideas and examples :https://github.com/ikwattro/KwattroNeo4jOGMBundlehttps://github.com/ikwattro/Neo4jUserBundleBasically steps are common and lot of information can be found in the Symfony documentation.Create your user entity base on the neo4jphp-ogm annotationsCreate a User provider -> securityCreate a User Manager that will retrieve the users when the authentication happensConcerning your last error concerning the authentication, by looking at the klaussilvera/neo4j-ogm-bundle last commit date, chance is high that it doesn't support neo4j authentication added in 2.2.You might want to disable the neo4j authentication for your first tests or fork the current repository and create a PR adding the authentication parameters in the ConfigDefinition of the bundle. 这篇关于在Symfony2中设置NEO4j的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 11:06