On Thu, Apr 7, 2011 at 4:36 AM, OnDistantShores <cameron.r...@gmail.com>wrote:
> I'm used to using MVC where models can serialise themselves (into an > array, which is easily JSON-able). > > I'm trying to build an API app where multiple views in multiple > modules serialise the same object. I can easily add that code to the > model, which works fine. However the problem arises when I try and add > a URL (created using url_for) to the serialised object. This only > seems to work from the view. > > So does this mean the serialisation logic really should go in the > view? If so I want to create a helper function called > serialiseMyModel($model) that can be called from the multiple views. > Where can I create this so that it's available to all the views? > > Disclaimer: you should take my advice with a grain of salt, as I'm pretty new to Symfony2. This sounds like a use-case for a Service to me ( see: http://symfony.com/doc/2.0/book/service_container.html ). You can put the logic in one spot, call $this->get('servicename')->foo in your controllers and pass the results (or whatever would be appropriate) to your views. As a (perhaps slightly useless) example, I recently wrote a service for "flattening" some Doctrine records on a project I'm working on. They're stored in such a way that they're very convenient to work with from the "admin" side of the app, but cumbersome in views on the "public" side. A birds-eye pseudo-view of the service looks like so: app/config/config.yml: -------------------------------- # blah blah .... services: record_flattener: class: My\SomeBundle\Services\RecordFlattener arguments: [ @doctrine.orm.entity_manager ] ================ src/My/SomeBundle/Services/RecordFlattener.php: -------------------------------- ... class RecordFlattener { ... public function __construct($em) { $this->em = $em; } .... public function getAll($type) { $items = Array(); foreach ($this->getRecordType($type)->getRecords() as $record) { $items[] = $this->flatten($record, $type); } return $items; } .... protected function flatten($record, $type) { $flattened = (object) null; $cls = get_class($record); $meta = $this->em->getMetadataFactory()->getMetadataFor($cls); ... // go on through the associations for this record, and the records // non-assoc fields and stick them on $flattened->$whatever return $flattened; } } ====================== src/My/SomeBundle/Controller/Default.php --------------------------------------------- ... class DefaultController extends Controller { ... public function index() { $items = $this->get('record_flattener')->getAll('item'); return $this->render( 'MySomeBundle:Default:index.html.twig', Array( 'items' => $items ) ); } ... } ======================= It was a little rough understanding what the services were supposed to be, probably mostly due to my unfamiliarity with Symfony (I never used 1.x) , but not bad at all for the stage of development Symfony2 is at. It's also possible that I'm abusing them here. I'm not sure if you can make what you want automagically available in the views easily( maybe making a service based off of whatever is rendering them and adding a parameter to it ) , nor am I sure that you should, but pulling out to a "global-to-controllers" service would at least cut down on code redundancy quite a bit ;) Also, if I'm way off base here, someone please let me know. I'm liking Symfony2 so far, but am really far from knowing what idiomatic Symfony2 codebases would end up looking like, other than "like an MVC framework geared towards webapps". -- If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com You received this message because you are subscribed to the Google Groups "symfony users" group. To post to this group, send email to symfony-users@googlegroups.com To unsubscribe from this group, send email to symfony-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/symfony-users?hl=en