跳至主要内容

博文

目前显示的是标签为“symfony2”的博文

jquery file upload with symfony2

for use this plugin, install jquery-file-upload in web install punk_ave bundle and this is my example: /** * @Route("/import",name="prospection_admin_import") * @Template() */ public function importAction(){ $fileUploader = $this->get('punk_ave.file_uploader'); $request = $this->getRequest(); $existingFiles=null; $editId = $request->query->get('editId'); if (!$editId){ $editId=$request->getSession()->get('editId'); } if ($editId){ $existingFiles = $fileUploader->getFiles(array('folder' => '/tmp/attachments/'. $editId)); if ($request->getMethod() == 'POST') { $type=$request->request->get("importType"); $importHandler=$this->get('addepi.prospection.import.handler'); $path=$this->get('kernel')->getRootDir()."/../web/uploads/tmp/attachments/".$editId."/originals/"; f...

dynamically change the form presentation

In this article, we discussed how to do it: http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html I also give my example : public function buildForm(FormBuilderInterface $builder, array $options) { $factory=$builder->getFormFactory(); $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($factory){ // ... add a choice list of friends of the current application user $address = $event->getData(); $form = $event->getForm(); if ($address instanceof Address && $address->getWebSite()!=""){ $url=$address->getWebSite(); }else{ $url= 'http://www.'; } $form->add($factory->createNamed('webSite','url',$url,array( 'required'=> false, 'attr' => array('class' => 'defaultInvalid'), 'label' => 'form.address.label.webSite...

css rewrite , concerned with less and yui

I have a problem really odd about css rewrite. suppose we have a css in AcmeTestBundle/Resources/public/css folder, named a.less. In this a.css we have a rule like: background: url(../images/buttons.png) no-repeat top left; which is the AcmeTestBundle/Resources/public/images/button.png, for some ugly reasons, if we use symfony2 assetic, the cssrewrite can not translate it correctly, so we use this commmand first ; php app/console assets:install and then in our layout.twig, we can use this: {% stylesheets 'bundles/acmetest/css/*.less' filter='lessphp,cssrewrite,?yui_css' output='css/test.css' %} {% endstylesheets %} well, the odd thing come, we get a web/css/test.css, and in this file, we have a image link like this: background: url((../../bundles/acmetest/images/buttons.png) no-repeat top left; if your site url is the root of the domain, no problem, like you have a domain name : www.abcefg.com. But,...

symfony2 PHP Warning: SessionHandler::close(): Parent session handler is not open when logout

this is an error introduced by php version under 4.11, https://groups.google.com/forum/#!topic/symfony-devs/Q-f0lKT7OZ8 and https://github.com/symfony/symfony/issues/5868 are the bug report, the solution for no way to upgrade  php is : logout: path: fos_user_security_logout target: / invalidate_session: false

sonata admin bundle problem

As well know, i have found it a long time ago and it never really solved. if you set this in your config.yml: framework: csrf_protection: enabled: false well ,you will receive this error: The option "csrf_protection" does not exist. Known options are: "virtual", "error_bubbling", "empty_data", "data", "data_class", "trim", "required", "read_only", "max_length", "pattern", "property_path", "by_reference", "error_bubbling", "error_mapping", "label", "attr", "invalid_message" and so on... This problem is reported for sonata admin group, but i didn't see the solution. so just remove the enabled false. Anyway, if you do not define the csrf protection, it will not apply automatically. So this will not be a really problem for the existing project.

symfony2 Jsonresponse

if you wanna to encode a json object as the json response, you must set all the property of this objec in public, or it will not work. Like this: class JsonResponseData{ public $msg; public $data; public $errMsg; public function setMsg($msg){ $this->msg=$msg; } public function get(){ return $this->msg; } public function setData($data){ $this->data=$data; } public function getData(){ return $this->data; } public function setErrMsg($errMsg){ $this->errMsg=$errMsg; } public function getErrMsg(){ return $this->errMsg; } } before i set them in private, and i always get a void response object. after i set them to public ,everything works fine.

property not found error

As far as I can tell there's a problem here, because any call to app/console initialises the container, which warms up Proxies (unless cache is primed), which depends on ORM metadata. Let say you switch branches and want to clear cache/metadata by calling app/console cache:clear or doctrine:cache:clear-metadata, that will result in ReflectionException if cached metadata no longer matches entities. I tried to finds ways around it and clarify it on IRC with no luck, so I'm raising this as a potential bug. this is a bug report, and today i got a similar one. après la mise à jour de nouveau, le module modération ne fonction plus, operation pour la mise à jour: composer dump-autoload --optimize php app/console assets:install php app/console assetic:dump --no-debug svn commit sur serveur: rm app/cache/prod -rf log: require(/home/admin/app/cache/prod/doctrine/orm/Proxies/__CG__AcmeModerationBundleEntityModerationObjectStatus.php): il ne trouve pas le factory, donc, php...

phpunit test get error: No tests found in class "xxx" , solution

if you get this error, like me use this: use Symfony\Component\HttpFoundation\Request; abstract class BaseControllerTestCase extends WebTestCase { ... } if you do not add the abstract infront of it, it will consider this class contains some test case, but in fact this class is not used to test, just a extend of webtestcase. So to avoid this error, you need add abstract for this class. Anyway, this class will never generate an object.

symfony2 unit test

how tu simulate a request without call a controller? This article give a solution even his basic idea is to fast the controller unit test. For example, if we need to test a service which is called by a controller. In the service we have some parmeters from the request, or have a request as parameter, so we need to generate the container before test this service. But the normal way of symfony2 unit test you need to generate a client and then send them to controller, so we need to find a way directly inject our container to the service. http://leftnode.com/entry/faster-symfony2-tests.html   see this. namespace Acme\DemoBundle\Tests\Unit\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\HttpFoundation\Request; class ControllerTest extends WebTestCase { public function testControllerAction() { $client = static::createClient(); $container = $client->getKernel() ->getContainer(); $request = new ...

eclipse run makegood as phpunit test for symfony2.2

well, as the title says, these articles give me the hint to install it. install phpunit for symfony2.2 is so simple: add "require-dev": { "phpunit/phpunit": "3.7.*" }, in your composer.json in symfony2.2 project. http://blog.loftdigital.com/running-phpunit-tests-in-eclipse-pdt install eclipse make good http://dev4theweb.blogspot.fr/2012/07/makegood-and-symfony2.html this one make makegood works with symfony2 if you get something like this: The  MakeGood View  notify now:  PHPUnit_Framework_TestCase class is not available. Fix… you need to add the phpunit folder in the include path(this is for windows), if in ubuntu, maybe see this: http://www.stuermer-benjamin.de/v12_1/eclipse-4-2-with-php-android-and-unit-testing/ and the makegood official user guild is here: http://piece-framework.com/projects/25/wiki/MakeGood_User_Guide_1_2_0

symfony2 transchoice:

in tranlate file, you must add a "'" for your transchoice string, but it is not needed for trans string. For example: list: create: company: Ajouter une société appointment: Ajouter une rendez-vous export: company: Exporter des entreprises text: vers un fichier texte website: vers un site web index: visit: '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples' call: '{0}There is no apples |{1} There is one apple |]1,Inf] There are %count% apples' If you remove the ', then symfony will tell you he can not find the proper trans string.

create a download link in symfony2

download_route : pattern : /download/ { filename } defaults : { _controller : YourBundle : Controller : download } And then in your controller, public function downloadAction ( $filename ) { $request = $this -> get ( 'request' ); $path = $this -> get ( 'kernel' )-> getRootDir (). "/../web/downloads/" ; $content = file_get_contents ( $path . $filename ); $response = new Response (); //set headers $response -> headers -> set ( 'Content-Type' , 'mime/type' ); $response -> headers -> set ( 'Content-Disposition' , 'attachment;filename="' . $filename ); $response -> setContent ( $content ); return $response ; } For generating download link check  Generating urls  section of the doc. for a nomal php , if the content is a file: if ( file_exists ( $file )) { header ( 'Content-Description: File Transfer' ); ...

get entity obj from form view in twig template

form . get ( 'value' ) is deprecated since symfony 2.1. Copy from Symfony\Component\Form\FormView : /* * @deprecated Deprecated since version 2.1, to be removed in 2.3. Access * the public property {@link vars} instead. */ public function get ( $name , $default = null ) .... so, I guess form . vars . value . youMethod () And if you get {{ form . vars . value }} is NULL. If you have form.element (not the form object itself) object, for example if you are overriding a form_row template that has passed the form.row object, you can get the Entity like this: {{ form . getParent (). vars . value . MyEntityMethod }}

symfony2 translator in formtype class

this should works, $builder ->add('select','choice',array( 'multiple'=>false, 'expanded'=>true, 'choices' => array('0'=>'form.search.label.all','1' => 'form.search.label.select','2'=>'form.search.label.unselect'), 'required' => false,)) this also: $builder->add('givenTakenEmail','email', array('label' => 'overview.marker.new.given_taken_email', 'read_only' => $readOnly, 'required' => true,) );

CSS LESS

Less 能让你更方便的写css,就像写程序一样,可以继承,可以设置变量。 请看这篇文章介绍如何在symfony2下安装less http://www.yves-astier.com/2012/09/20/symfony-2-comment-integrer-less/ and http://www.symfony-grenoble.fr/80/utiliser-less-avec-assetics-de-symfony2/ 这是它的官网: http://lesscss.org/ in syfmony2 , how to combine cssrewrite, yui and less? here is the answer: {% stylesheets debug=false filter='lessphp,?cssrewrite,?yui_css' output='css/moderation.css' "bundles/acmemoderation/css/*.less" %}