Created
October 30, 2013 17:51
-
-
Save tiraeth/7237039 to your computer and use it in GitHub Desktop.
Example usage of mach/silex-rest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"mach/silex-rest": "dev-master" | |
}, | |
"autoload": { | |
"psr-0": { "": "src/" } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That the point for
convert('user', function($user,
You are never use this part in the example
and even override it in the foreach.