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.
Related
I already tried http://service/?wadl to get a wadl file, but nothing is returned.
I read there isn't really a standard for wadl files in REST (but there is one for SOAP's WSDL), but I thought maybe it would exist and be web-service framework specific to PHP Symfony.
Is there a default wadl url for pulling a list of function signatures?
There would be, if you create it. Symfony does not define URLs for you, it's just a framework.
A Symfony application is not necessarily a REST API application, or even a Web application. It could be a console application, a message consumer, or anything else. It's not a given that "method signature" should be publicly available via any kind of endpoint.
If you want to an endpoint providing some functionality (say, a WADL endpiont, or a WSDL endpoint, or an anything endpoint), you need to provide it by creating and configuring it.
Some libraries built on top of Symfony like Api-Platform or NelmioApiDoc can provide some automatic or semi-automatic documentation for an API built on top of symfony, but how to configure each would depend on one actually using these libraries, and what's one's use-case.
I am working on a web application built with php and zend framework 2. I come from a Java EE background. It seems to me that for each http request the whole zend application stack is being rebuilt, reading a load of config files from disk, constructing all my services and whatnot. Is this correct? If so this seems rather strange and inefficient to me compared to the Java EE approach of having a load of application services that are initialized on web server start up and then have a lifetime across many requests. Seeing as the browser is making lots of little Ajax requests isn't this php/zend approach horribly slow? Do I need a paradigm shift in the way I approach web application design?
ZF2 can use caching to improve performance , the configuration also
can be cached as well .
any service or class in ZF2 shouldn't be constructed on every request , for example a db connection (ZF2 won't try to connect to db unless you are really doing something to the db server)
another example :
suppose you had an REST API , you need to use on one controller only , no need to construct this api on every request , you alternatively ask you module's service manager to construct that object for you on that controller only .
take a look at this blog , it might help you : http://www.masterzendframework.com/articles-2/zend-framework-2-core-concepts-understanding-dependency-injection
or http://akrabat.com/ or the ZF2 Team leader : Matthew Weier O'Phinney http://mwop.net/
(if I understand your question, sorry if not)
it's correct that php is not running in an environment, like java does.
This means that your php application is beeing initialized on each request.
It seems to me that for each http request the whole zend application stack is being rebuilt
But this is not correct. The initialization of your php- / zf2- application is not that heavy as you may think, when you're coming from java.
Don't think of something like a tomcat server being rebooted on each request. The whole Zend Framework is also not beeing loaded and your application classes aren't as well.
Only the classes that are needed for your specific request are being loaded.
PHP Frameworks make use of Autoloaders for this, so if you call new MyClass() in your app, that class will be loaded. And even the file containing "MyClass" is not being loaded from disc in many cases, but from ram-memory or bytecode cache.
If your server is set up correctly, the zf2 skeleton application for example will be loaded in a few milliseconds.
What about your services that need to be ready for your application?
They're nothing more than a "key" in an associative array, inside your configuration. When your application needs a service for your specific request, it will find the entry in your configuration array extremely fast. The Service will be initialized from a factory method or a factory class only for this request, but only the specific one which is needed.
Facebook is using php as well. It doesn't need a minute for each request right? If php worked the way you're thinking of it at the moment, it would take minutes or hours I guess - or just end up in a timeout :)
On the same server I have a Restler 3.0 API server and also a CakePHP 2.3 application, I want to be able to use CakePHP controller functions from the Restler app. Of course I don't want CakePHP to do any rendering, just to deal with the data.
I considered just doing a https request to the CakePHP app from the Restler api, but this seemed pretty inefficient for the client of the Restler server. I also considered using RabbitMQ to do RPC between the apps but RPC in PHP seemed like too much complexity for something i'm trying to keep simple.
Ideally in Restler i might have something like this:
<?php
class Content {
function post() {
// CakePHP stuff:
$data = array('title'=>'fake data');
$this->Content->create();
if ($this->Content->save($data)) {
return 'ok';
}
}
}
I'm completely open to any good ideas on the best way to achieve this integration.
Whether this is possible really depends on the nature of your environment.
Considerations:
Does your controller use other cake framework components(Controllers, Models, components, etc.)? You might be hosed if this is the case because cake is convention oriented. Without the cake plumbing to interpret those conventions they are meaningless.
If you just need to repurpose some non-cake specific PHP logic you could always refactor it into its own php class or even just a .php file with some functions in it and share them between your Restler App and your Cake Application.
is there any way I can use Zend only for server process?. I want to have all my client process in javascript and the server process in php using Zend framework or any other framework.
Any ideas are welcome.
Zend is a component library that let you do php on the server side - So yes! you can use it to handle all your clients' process.
BTW, it's a good practice to leverage ZendFramework MVC so you will start with great concept/project structure etc'.
Good luck!
The answer is: of course!
ZF is a server-side framework. It's completely agnostic about it's output format. You'll find you have no problem outputting json, or xml, or whatever format you like.
While it's typical to have your application output HTML pages via Zend_View/Zend_Layout, there are other components available to facilitate sending non-HTML output..
If you're doing a fancy javascript-driven client, you might consider going the RESTful route with Zend_Rest_Route, and/or Zend_Rest_Controller.
In your comment, you mention concern about Zend_Form. You're free to ignore Zend_Form -- though you might want to reconsider. It's quite possible to leverage the filtering/validation functionality of Zend_Form (define a form, and just use populate(), isValid(), etc) without ever rendering a form. I did a project not long ago that was a single-page app using ExtJS on the client side, which which talked to a set of RESTful services in JSON provided by ZF. I used Zend_Form extensively just for filtering/validating data.
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.