I need to load entities from a REST web service in the same way as I would from a database using Doctrin ORM.
Creating REST web services in Symfony is really well documented but I canlt see anythign about consuming them.
I've written losts of apps in straight PHP that load data from REST services but I'm sure there must me a bundle in Symfony that will do it for me in the same way as Doctrin ORM allows me to map to and from databases.
Is there something I'm missing or do I need to write my own custom repsitories to load entities from web services?
you can use a PHP HTTP client framework like guzzle, take a look at :
https://github.com/misd-service-development/guzzle-bundle
Related
I recently started learning PHP and Symfony 4 and I developed an app. Now my next task is to change the architecture of the project, from MVC to client-server. More exactly, I need to have my templates and controllers in an application and the business logic in another application. Any ideas how I can do this? I can't even figure out how to run 2 different symfony projects simultaneously.
A client-server application is an application base on a communication model call client-server: wiki
MVC is a way to develop and structure application: wiki
But i understand your needs. You want to keep a symfony app but you also want to put all the logic view and front end features into a separate app (like React or Vue)
Your Symfony App will still use MVC pattern, just you'll replace what the controller give to the view.
The best way is to turn the Symfony App into API with normalize view data like JSON or XML. Those data will feed your front app.
To achieve this you can use this bundle, which turn your model layers into API: API Platform
I'm currently designing a new web application based on Symfony Framework that will serve both web and mobile accesses. For illustration purposes I will use the default AcmeDemoBundle from Symfony`s framework to elaborate my question.
So, DemoBundle gives me the following route:
/hello/{name}
So far so good. Now, I want to implement an API in this project that will serve mobile apps, so the route for the same controller as the route above whould be, for instance:
/api/v1/hello/{name}
My doubt is: what's the best way of doing that without replicating code? I intend to use the FOSRestBundle for the API and I know that he handles rendering HTML views to, but I'd like to separate the API routes from the web as I put before. Maybe should I create a new bundle for the API?
Create a bundle for the API is not a good practice for the simple reason that you can't easily re-use the bundle in an other project.
For example, you create PostBundle and his API bundle, APIPostBundle for your A project.
If you want to use that bundle for a B project, you need PostBundle and APIPostBundle. You created dependency between two bundles and it's a bad practice (Component-Based Development).
So, you have to generate an independency bundle with a strength architecture who can allow you less modification if you have to.
Use a service for the logical instructions. Then, the controller/APIController can manage the route and will call the service methods. By that way, you can just modify the service without editing the controllers.
So, your architecture looks like that:
PostBundle
Controller
- PostController
- APIPostController
Service
- PostService
- PostServiceInterface
I have a web application which has been developed with symfony 1.4. I have a pretty large code base (and growing). Circa 80,000 lines of code (actions, forms, models, templates etc.)
I'm using the default doctrine version which ships with symfony 1.4.
I've just started developing a mobile version using Sencha touch. I don't wish to use symfony for the REST web services because:
REST services in Symfony 1.4 is not great. For example, If i want a PUT request I have to pass a 'sf_method' parameter specifiying that the request method is PUT. This isn't true REST and it's not ideal for Sencha touch.
I don't need all of the unnecessary symfony functionality(for example the plugins that are autoloaded in the ProjectConfiguration file, the form framework etc.) that you'd use to develop a standard web app. All I need is to define my REST routes and return the specified JSON (as everything that needs to be returned for Sencha touch will be JSON)
I want to keep my mobile app as bloated-free, efficient and quick as possible. And unfortunately for this task, Symfony 1.4 would not be the best choice for using as the backend architecture for my mobile app. If I had chosen symfony2 (it was in it's beta phase, alas) it would be a different story as symfony2 supports true REST functionality. What I do need, however, is the ability to use my current doctrine models (I have circa 90 models) in a chosen REST framework.
Basically, in a nutshell what I need is as simple as this:
Call a rest route->Query my doctrine models->return the JSON without using symfony.
So my question, what would be your advice? I don't want this to be a question of which is the best PHP rest framework, however, what I would like to know is what would be a good REST framework which i can develop efficiently and quickly REST service, make use of my doctrine models and is easily extendable.
Here at my employer, I've created a rather big application with a ExtJS frontend, and Symfony 1.4 backend. And two be honest, I don't feel limited by Symfony 1.4 in any way?
First of: I created my own base controller class (which extends sfActions). This controller can handle (render) different types of data. It has generic handling for Doctrine_Query, Doctrine_Collection, Doctrine_Model and array types.
Also the plugins make me help organize the code, and in some cases plugins are shared between differend projects, so that's also a big plus.
And the extra functionality like forms: it's only prepared for you in the autoloader, you don't have to use it. And I don't think it causes any real performance issues (at least not for me). But I like to use the extra sfValidator framework, to make sure data are correct.
The only real "problem" is indeed the HTTP REST-ful commands, especially PUT and DELETE. I just worked around this problem by generating a controller for each 'manageable' model, and implement specific get, list, create, update and delete actions. So when I would like to manage an Object, I call the objects controller, which has executeCreate, executeUpdate and executeDelete actions.
The reason I read, was that Symfony didn't and couldn't implement this feature because PHP has really bad support this. I don't know if this is true, but if this is your only 'real' issue, you could try to fix this in the Symfony core.
So my advice:
If the raw performance is your problem: try profiling your code, install a opcode (APC) cache, and profile your code (yes, that's double).
If the HTTP PUT command is your problem: I would either work around this (that's the way I solved it), or try to fix it in the core.
Does anybody know a good web framework that includes an ORM mapper and allows straight forward implementation of web services? I'm looking for a framework written in PHP or C++. I'm looking for the following features (not all of them required, some will do nicely)
data definition in one place used by database and web service
WSDL generation
XML output/JSON output
boilerplate code generation
So what I would like is a framework that let's me specify the objects, the web service functions on those objects and then generate everything that is required leaving me to fill the business logic (connecting the database to the web service).
Anything like that out there?
Background information for why I need this:
I'm looking into creating a web project: the client is a rich web application that fetches all its data using AJAX. It will be completely custom made using only a low level javascript library. The server back end is supposed to serve static content and javascript (basically the rich web application) and to provide a RESTful web service API (which I would like to implement using aforementioned framework).
I would recommend using Zend_Framework and replacing Zend_Db with Doctrine as your ORM.
You can use Zend_Service to consume webservices and Zend_Rest_Controller to serve a REST API.
There are some good screencasts on integrating Doctrine and Zend here. If you have alot of PHP experience, it shouldn't take very long to integrate. I believe there are even some sample integrations on Github.
I'm developing a zend framework app that's just going to act as a web service. I have no need to ever output HTML at any point in the application and don't even want the overhead of creating empty view files.
I want my app to output XML by default, JSON if requested (via the format parameter would be fine).
Is there any way to do this without explicitly defining the context switching rules in the init() part of every controller?
If you're going to be providing JSON, SOAP or XML-RPC, you're probably better off using Zend_Json_Server + Zend_Soap_Server instead of Zend_Controller_Action. Both the JSON and SOAP server classes can consume the same server class. No need for the overhead of routing, etc.
Matthew Weier O'Phinney's (Zend FW lead), site has a great post detailing the proper way of doing this: Exposing Service APIs via Zend Framework
you could try doing the context switch using a Zend_Controller_Front plugin on preDispatch.