php - Symfony 2 Service How To Feed My Own Data Into My Service? -


ok, have set few services in pass not many, knowledge them limited.

i have set so,

upload:     class: mybundle\classes\upload     arguments: [@doctrine.orm.entity_manager, %formdata] 

so trying set own class upload file data. doing has there number of controllers need upload data. have tried using, @formdata , $formdata, still not passing form data service?

form data being set follows:

// getting file non mapped, form element $formdata = $form->get('file')->getdata(); 

this how called in controller,

$this->get('upload', $formdata) 

however formdata seems dump variable name , not pass data class.

so upload class set this:

class upload {     private $data;      public function __construct(entitymanager $em, $data) {         $this->em = $em;         $this->data = $data; <-*note 1     }      public function uploadfile() {         // ...do , save path/data file entity...     } }  

and yes loading 'entitymanager' namespace class...

so why $this->data not contain form data passed it?

note 1: doing var_dump() on , displaying $formdata or @formdata.

all welcome....

thanks.


update

what thinking set normal class like,

$uploaddata = new upload($data, $em); 

and pass in data , $em?

is best why set up?

thanks

you close. idea behind service created is reusable - wouldn't want directly inject single form's data in service's constructor. thing want inject other services might need - in case, doctrine entity manager.

that service created before of controller code executed, have no way of passing form data constructor anyway. symfony uses dependency injection of heavy lifting you, passing in doctrine entity manager don't have manually that, trying. service definition going simplified:

upload:     class: mybundle\classes\upload     arguments: ["@doctrine.orm.entity_manager"] 

the defiinition of service simplified well:

use doctrine\orm\entitymanager;  class upload {     private $em;      public function __construct(entitymanager $em) {         $this->em = $em;     } } 

you have option of creating setter form data, it's better pass uploadfile() function in service, so:

class upload {     public function uploadfile($formdata)     {         // form data here     } } 

then can following in controller:

$this->container->get('upload')->uploadfile($form->get('file')->getdata()); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -