Created
January 17, 2014 19:38
-
-
Save j0shua/8480007 to your computer and use it in GitHub Desktop.
Symfony REST API set-up / quickstart
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
install + required bundles: | |
> composer create-project symfony/framework-standard-edition rest 2.2.0 | |
> cd rest | |
> composer require jms/serializer-bundle @stable | |
> composer require friendsofsymfony/rest-bundle @stable | |
> app/console --verion | |
[symfony version 2.2.0 - app/dev/debug] | |
> app/console generate:bundle | |
.... | |
- register the bundles in app/AppKernel.php > registerBundles() | |
new JMS\SerializerBundle\JMSSerializerBundle(), | |
new FOS\RestBundle\FOSRestBundle(), | |
create an Entity to represent our Resource | |
> app/console doctrine:generate:entity | |
follow prompts to create fields | |
== edit config.yml | |
fos_rest: | |
format_listener: | |
prefer_extension: false | |
view: | |
view_response_listener: true | |
- Add section - | |
sensio_framework_extra: | |
view: {annotations: false} | |
router: {annotations: true} | |
== edit routing.yml | |
blah_demo: | |
resource: "@DemoBundleName/Controller/" | |
type: annotation | |
prefix: / | |
and add resource routing too ... | |
users: | |
type: rest | |
resource: PackageName\DemoBundle\Controller\UsersController | |
create the UsersController | |
use FOS\RestBundle\Controller\Annotations\View; | |
// to auto load resource like user | |
use Sensio\Bundle\FrameworkExtraBundle\Configration\ParamConverter; | |
class UsersController externds Controller | |
/** | |
* | |
* @return array | |
* @View()( | |
*/ | |
public function getUsersAction() | |
{ | |
$users = $this->getDoctrine()->getRepository('DemoBundle:User')->findAll(); | |
return array('users' => $users); | |
} | |
/** | |
* @param User $user | |
* @return array | |
* @View()( | |
* @ParamConverter("user", class="DemoBundle:User") | |
*/ | |
public function getUserAction(User $user) | |
{ | |
return array('user' => $user); | |
} | |
== check routes: | |
> app/console router:debug | |
... list of routes... | |
== setup dummy data using fixtures download | |
> composer require doctrine/doctrine-fixtures-bundle dev-master | |
== add to appKernel | |
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle() | |
== create fixture file bundledir/DataFixtures/ORM/LoadUserData.php | |
use Doctrine\Common\DataFixtures\Doctrine; | |
use Doctrine\Common\DataFixtures\FixtureInterface; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use Namespace\BundleName\Entity\User; | |
class LoadUserData implements FixtureInterface | |
{ | |
public function load(ObjectManager $manager) | |
{ | |
$alice = new User(); | |
$alice->setUsername('alice') | |
$alice->setEmail("[email protected]"); | |
.... | |
$anotheruser = new User(); | |
// set fields for other user | |
$manager->persist($alice); | |
$manager->persist($anotheruser); | |
$manager->flush; | |
} | |
} | |
== edit parameters.yml | |
parameters: | |
... | |
database_name: demo_rest | |
database_password: root | |
> app/console doctrine:database:create | |
> app/console doctrine:schema:create | |
> app/console doctrine:fixtures:load | |
== change fields being exposed in the view (don't send password) by creating whitelist | |
bundle/Resources/config/serializer/Entity.User.yml | |
NamespaceName\DemoBundle\Entity\User: | |
exclude_policy: ALL | |
properties: | |
id: | |
expose: true | |
username: | |
expose: true | |
email: | |
expose: true | |
== clear cache | |
> app/console cache:clear --env=dev |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment