$_POST
and $_FILES
).
Net_HTTP_Request
class. Its main features are: convenient access to the parameters and fields of the request header, uploads as file objects, etc.
1 <?php <br>
2 $ id = $ request [ ' id ' ] ;<br>
3 $ auth = $ request -> header [ ' Authorization ' ] <br>
4 foreach ( $ request [ ' upload ' ] as $ line ) { /* ... */ } <br>
5 $ stores = $ request [ ' upload ' ] -> copy_to ( $ permanent_path ) ;<br>
6 ?> <br>
Net_HTTP_Response
class, which provides convenient work with response headers, body as a string, iterator or file object, etc.
1 <?php <br>
2 $ file = IO_FS :: File ( $ path ) ;<br>
3 return Net_HTTP :: Response () - > <br>
4 status ( Net_HTTP :: OK ) - > <br>
5 content_type ( MIME :: type_for_file ( $ file )) - > <br>
6 body ( $ file ) ;<br>
7 ?> <br>
1 <?php <br>
2 interface WS_ServiceInterface { <br>
3 public function run ( WS_Environment $ env ) ;<br>
4 } <br>
5 ?> <br>
run()
method execution is an object of the Net_HTTP_Response
class. The WS_Environment
class WS_Environment
designed to exchange information between service objects; each service can create, read, or write the value of an environment parameter. By default, the environment contains the $env->request
element of the $env->request
class.
WS_ServiceInteface). Its
run()
method delegates processing to a user-defined resource object created by the dispatcher based on the description of the application’s resource set.
1 <?php <br>
2 $ companies = WS_REST_DSL :: Application () - > <br>
3 begin_resource ( ' company ', ' App.WS.Company ', ' company/{name:[a-zA-Z][a-zA-Z-]+} ' ) - > <br>
4 sublocator ( ' blog ' ) - > // - <br>
5 sublocator ( ' vacancies ' ) - > // - <br>
6 for_format ( ' html ' ) - > <br>
7 index () - > // /company/Techart/ - <br>
8 end - > <br>
9 end - > <br>
10 begin_resource ( ' blog ', ' App.WS.Blog ', null ) - > <br>
11 sublocator ( ' entry ', ' {\d+:id} ' ) - > <br>
12 for_format ( ' html ' ) - > <br>
13 get_for ( ' {page_no:\d+} ', ' index ' ) - > // /company/Techart/blog/5.html - <br>
14 post () - > // - create() <br>
15 index () - > // /company/Techart/blog/ - <br>
16 end - > <br>
17 for_format ( ' rss ' ) - > <br>
18 get ( ' index_rss ' ) - > // /company/Techart/blog/index.rss - RSS- <br>
19 end - > <br>
20 end - > <br>
21 begin_resource ( ' entry ', ' App.WS.Entry ', null ) - > <br>
22 for_format ( ' html ' ) - > <br>
23 index () - > // /company/Techart/blog/82715/ - <br>
24 get_for ( ' print ', ' print_version ' ) - > // /company/Techart/blog/82715/print.html - <br>
25 put () - > // , - update() <br>
26 delete () - > // , - delete() <br>
27 end - > <br>
28 end - > <br>
29 begin_resource ( ' vacancies ', ' App.WS.Job ', null ) - > <br>
30 for_format ( ' html ' ) - > <br>
31 index () - > // /company/Techart/vacancies/ - <br>
32 end - > <br>
33 end - > <br>
34 end ;<br>
35 ?> <br>
$env
, $request
and $format
. If the argument name is not included in the parameter set of the template and is not a predefined parameter, null
substituted.
$format
parameter. Formats can be specified both for individual methods and for whole resources.
1 <?php <br>
2 // - <br>
3 class App_WS_Resource { <br>
4 protected $ env ;<br>
5 protected $ db ;<br>
6 <br>
7 public function __construct ( WS_Environment $ env ) { <br>
8 $ this -> env = $ env ;<br>
9 $ this -> db = $ env -> db;<br>
10 } <br>
11 } <br>
12 <br>
13 // blog <br>
14 class App_WS_Blog extends App_WS_Resource { <br>
15 <br>
16 public function index ( $ page_no = 1 ) { /* $page_no URL */ } <br>
17 <br>
18 public function index_rss () { /* RSS- */ } <br>
19 <br>
20 public function entry ( $ id ) { <br>
21 // - <br>
22 // <br>
23 if ( $ entry = $ this -> db -> blog -> entries [ $ id ]) <br>
24 return new App_WS_Entry ( $ this -> env, $ entry ) ; <br>
25 } <br>
26 <br>
27 public function create () { /* */ } <br>
28 } <br>
29 <br>
30 // entry <br>
31 class App_WS_Entry extends App_WS_Resource { <br>
32 protected $ entry ;<br>
33 <br>
34 public function __construct ( WS_Environment $ env , App_DB_Entry $ entry ) { <br>
35 parent :: __construct ( $ env ) ;<br>
36 $ this -> entry = $ entry ;<br>
37 } <br>
38 <br>
39 public function index () { /* */ } <br>
40 public function print_version () { /* */ } <br>
41 public function update () { /* */ } <br>
42 public function delete () { /* */ } <br>
43 } <br>
GET /api/news/stories/most_popular
$db->news->stories->most_popular->select()
POST /api/news/stories/
$db->news->stories->insert($story)
PUT /api/news/stories/15
$db->news->stories->update($db->news->stories[15]);
Source: https://habr.com/ru/post/83442/