I am building an application which uses an API to get/save data rather than a MySQL database.
The application is acting as an isolated front end.
I really like how the Laravel Eloquent ORM works but it seems to only work with the databases specified in the documentation.
Would it be possible to use Eloquent to interact with data on an API over HTTP/HTTPS?
No, it's not possible, mainly because each HTTP API has its own "format" so there's no easy way to create a tool that can be compatible with all existing APIs to provide a standard interface like Eloquent does.
What you can do however is create an extension that would interact with the API and provide the same methods that Eloquent does.
If you need libraries for consuming an API, you can try Guzzle or Httpful, both installable via Composer.
Related
We are migrating our project from custom CMS to Symfony 5.1. Instead of Database, we are using REST API. Currently we are using Entities without connecting them to any database (with no #ORM annotation or configuration file), and we put all REST logic inside a Repository folder
Currently, if we save our entity in SESSION
$example = new Example();
$request->getSession()->set('example', $example);
On every next request, we will get error:
Class "App\Entity\Example" is not a valid entity or mapped super class.
Of course, this could be fixed if we put correct #ORM annotations, but then it requires a database.
Can this be fixed, and if it's not possible, what would be proper architecture for this scenario?
Let doctrine be the tool to manage the data. In fact that there is no database, you will need something similar to let doctrine manage the data of the REST API.
Take a look at this bundle:
https://github.com/CircleOfNice/DoctrineRestDriver
Edit:
In addition to one of the comments, the DoctrineRestDriver is not maintained anymore. In fact, the bundle does not support Symfony 5.x.
To use the functionality of DoctrineRestDriver you could downgrade your symfony to the supported 4.x.
Your architectur to use doctrine to manage your rest-api can be solved by using the DoctrineRestDriver package.
If you don’t want to downgrade your symfony, you will have to implement your own „data-manager-solution“, to use the api as your database.
But I think the architectural problem with the rest api doesn’t relate directly to your current error. We need more code to understand, where your specific mapped super class error is from (repository, entity).
In Laravel 5.5+, we can use API resource to format our API response in a way we want. I want to create API resource in Laravel 5.4 project which I can't upgrade to 5.5. Is there any way to use API resource in Laravel 5.4?
If not, what is the best way to format response and how to properly format responses while creating an API with Laravel? What are the tips and best practices to create API in Laravel version 5.4, 5.3, 5.2 etc?
I would highly recommend using Fractal by the League of PHP:
http://fractal.thephpleague.com/
The concept is the same (typically a "resource" is called a transformer). They tackle both single items and collections and act as a simplification layer between the actual data and what you want presented through your endpoint.
I have been using this in 5.0 through to 5.4 without issue.
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
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.
We are looking to build a PHP web application to manage data that is available only through a 3rd party REST like interface.
We would like to use an existing PHP framework to do this, we're considering cakephp and codeigniter, but are open to other options.
We expect the biggest problem will be creating models that utilise the REST API as a data source instead of a local database.
I've written a base ReST datasource plugin for CakePHP and am using it in lots of other plugins that interact with ReSTful APIs. Plugins I've released so far are YahooGeoPlanet and bit.ly and I'm also currently working on ones for Google Apis (YouTube and Analytics), Twitter, Twitpic and Facebook. They use the CakePHP's excellent HttpSocket class that deceze mentions.
Have a look at those CakePHP plugins and you should be able to see how simple but powerful they are.
You might look at Zend_Rest_Client, which I've never used, but appears worth a look.
Failing that, Zend_Http_Client is pretty solid for abstracting away all the curl ugliness.
Either one can be used independently of the rest of the Zend Framework stuff, and should interoperate just fine with other frameworks.
You can implement it as a Datasource in Cake, which to the Model and therefore to the rest of the application behaves exactly as a database would. Internally the Datasource can dynamically fetch the data using cURL or the Cake HttpSocket. Have a look at the Twitter API example.