Skip to content

Instantly share code, notes, and snippets.

@tiraeth
Created October 30, 2013 17:51
Show Gist options
  • Save tiraeth/7237039 to your computer and use it in GitHub Desktop.
Save tiraeth/7237039 to your computer and use it in GitHub Desktop.
Example usage of mach/silex-rest
{
"require": {
"mach/silex-rest": "dev-master"
},
"autoload": {
"psr-0": { "": "src/" }
}
}
<?php
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Silex\Provider\ServiceControllerServiceProvider;
use Mach\Silex\Rest\ApplicationAwareController;
use Mach\Silex\Rest\Provider\RestApplicationServiceProvider;
require_once './vendor/autoload.php';
class UsersController extends ApplicationAwareController
{
private $users;
public function __construct(Application $app, $users)
{
parent::__construct($app);
$this->users = $users;
}
public function cget(Request $request)
{
return $this->json($this->users);
}
public function get(Request $request, $user)
{
return $this->json($user);
}
}
$app = new Application();
$app['debug'] = true;
$app->register(new ServiceControllerServiceProvider());
$app->register(new RestApplicationServiceProvider());
$users = array(
array('id' => '01', 'username' => 'foo'),
array('id' => '02', 'username' => 'bar'),
);
$userResource = $app['rest']->resource('/users', new UsersController($app, $users));
$userResource->convert('user', function($user, Request $request) use ($users) {
$id = $request->attributes->get('id');
foreach ($users as $user) {
if ($user['id'] == $id) {
return $user;
}
}
return null;
});
$userResource->assertId('\d{2}');
$app->run();
@mmuruev
Copy link

mmuruev commented Oct 31, 2013

That the point for
convert('user', function($user,
You are never use this part in the example
and even override it in the foreach.

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