Skip to content

Instantly share code, notes, and snippets.

@zircote
Created March 22, 2013 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zircote/5224386 to your computer and use it in GitHub Desktop.
Save zircote/5224386 to your computer and use it in GitHub Desktop.
I am not familiar with cake; however based on the example I was given this should produce the desired result.
<?php
use Swagger\Annotations as SWG;
App::uses('AppController', 'Controller');
/**
* Users Controller
* @SWG\Resource(
* apiVersion="0.2",
* swaggerVersion="1.1",
* basePath="http://api.myhost.local/api",
* resourcePath="/users"
* )
*/
class UsersController extends AppController {
/**
* view method
* @SWG\Api(
* path="/user.{format}/{id}", description="Operations for users",
* @SWG\operations(
* @SWG\operation(
* httpMethod="GET", summary="Find user by ID",
* responseClass="User", nickname="getUserById", notes="Returns a user based on ID",
* @SWG\parameters(
* @SWG\parameter(name="id",
* description="The id that needs to be fetched. ",
* paramType="path", required="true", allowMultiple=false, dataType="string"
* )
* ),
* @SWG\errorResponses(
* @SWG\errorResponse(code="400", reason="Invalid id supplied"),
* @SWG\errorResponse(code="404", reason="User not found")
* )
* )
* )
* )
*
* @throws NotFoundException
* @throws ValidationException
* @param string $id
* @return void
*/
public function view($id = null) {
// actual code redacted
}
}
/**
* @package
* @category
* @subpackage
*
* @SWG\Model(id="User")
*/
class User
{
/**
* @var string
* @SWG\Property(name="id",type="string")
*/
protected $id;
}
@zircote
Copy link
Author

zircote commented Mar 22, 2013

A good resource for working examples would be the test fixtures in the project also.

@alexmcroberts
Copy link

I found I had to modify this code slightly:

<?php
use Swagger\Annotations;
use Swagger\Annotations\Api;
use Swagger\Annotations\ErrorResponses;
use Swagger\Annotations\ErrorResponse;
use Swagger\Annotations\Operation;
use Swagger\Annotations\Operations;
use Swagger\Annotations\Parameters;
use Swagger\Annotations\Parameter;
use Swagger\Annotations\Resource;
App::uses('AppController', 'Controller');

and change any reference to @SWG\Api etc to @Api - this applies to all @swg references e.g. @swg\Resources, @swg\Parameters, @swg\Parameter

I found running the .phar file to be the most successful:

php /var/www/cakesite/app/Vendor/zircote/swagger-php/swagger.phar -p ./Controller -o /var/www/cakesite/app/webroot/api-docs/generated -f -i /var/www/engage/app/webroot/index.php &> ~/Documents/tmp.html

The &> is used to send all output from the terminal to a tmp.html in ~/Documents, so I can load it up in my browser to check for any feedback. This is useful as it shows CakePHP errors, if Debug > 0 in app/Config/core.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment