Skip to content

Commit 380e488

Browse files
author
Allan Simon
committed
added code explained in the Part 1, now we have an api call GET /api/articles/{id}
1 parent b610663 commit 380e488

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

MyApplication/app/AppKernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function registerBundles()
1616
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
1717
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
1818
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
19+
new FOS\RestBundle\FOSRestBundle(),
1920
new AppBundle\AppBundle(),
2021
);
2122

MyApplication/app/config/config.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ framework:
2424
handler_id: ~
2525
fragments: ~
2626
http_method_override: true
27+
serializer:
28+
enabled: true
2729

2830
# Twig Configuration
2931
twig:
@@ -71,3 +73,12 @@ swiftmailer:
7173
username: "%mailer_user%"
7274
password: "%mailer_password%"
7375
spool: { type: memory }
76+
77+
fos_rest:
78+
view:
79+
view_response_listener: 'force'
80+
formats:
81+
json: true
82+
format_listener:
83+
rules:
84+
- { path: ^/api, priorities: [ json ], fallback_format: json, prefer_extension: true }
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
app:
2-
resource: "@AppBundle/Controller/"
3-
type: annotation
1+
blog_api:
2+
type: rest
3+
prefix: /api
4+
resource: "@AppBundle/Resources/config/api-routing.yml"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace AppBundle\Controller;
4+
5+
use FOS\RestBundle\Controller\FOSRestController;
6+
7+
class ArticlesController extends FOSRestController
8+
{
9+
10+
public function getArticleAction($id)
11+
{
12+
return array('hello' => 'world');
13+
}
14+
15+
}
16+
17+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
blog_api_articles:
2+
type: rest
3+
resource: "@AppBundle/Controller/ArticlesController.php"
4+
name_prefix: api_articles_

MyApplication/web/app_dev.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010
// This check prevents access to debug front controllers that are deployed by accident to production servers.
1111
// Feel free to remove this, extend it, or make something more sophisticated.
12-
if (isset($_SERVER['HTTP_CLIENT_IP'])
13-
|| isset($_SERVER['HTTP_X_FORWARDED_FOR'])
14-
|| !(in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1')) || php_sapi_name() === 'cli-server')
15-
) {
16-
header('HTTP/1.0 403 Forbidden');
17-
exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
18-
}
12+
//if (isset($_SERVER['HTTP_CLIENT_IP'])
13+
// || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
14+
// || !(in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1')) || php_sapi_name() === 'cli-server')
15+
//) {
16+
// header('HTTP/1.0 403 Forbidden');
17+
// exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
18+
//}
1919

2020
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
2121
Debug::enable();

0 commit comments

Comments
 (0)