• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RESTful Sub Resource Handling Url

#1
hi  let jump in the  problem 
 i m  writing  Restful Api beck end  With Ci4 
if have two controller extend from ResourceController contact And ContactFile

how manger URL for sub Resuoures 

http://api.example.com/contact /* get*/ 
http://api.example.com/contact /* post*/  
http://api.example.com/contact/{id} /* put*/
http://api.example.com/contact/{id} /*delete */


this code is user for contactController
PHP Code:
$routes->resource('contact'); 
=> contactfile is sub Resource  
i need these urls bellow according to  restful api rules  


http://api.example.com/contact /{id}/contactfile* get*/ 
http://api.example.com/contact /{id}/contactfile* post*/  
http://api.example.com/contact/{id} /contactfile{id}/* put*/
http://api.example.com/contact/{id} /contactfile/{id}/*delete */

and this code is no  good  for sub resource 

Code:
$routes->resource('contactfile');


this is work but not best way handle  request   it need two time request for one for Parent(contactCtl) then childern(contacFileCtl) 


here is  code with angular 10  Get Http Request  its nested request 
 
 

Code:
public getSingelPost(id: number) {
  
    return this.http.get('http://localhost:8080/contact/1' + id).pipe(map((info: any) => {
     // console.log(info);
      return info.data;
    }),switchMap((info: any) => {
      // console.log(info);
      return this.http.get('http://localhost:8080/contact/1/contactfile/1');
    }), catchError(error => {
      console.log(error);
      return throwError('erors');
    }));
  }





please help me  thnak ci funs
Reply

#2
i find way to manage nested resource

first go to this link
    https://restfulapi.net/resource-naming/
to understand restful api then decide what you want to follow . i think its up to you
which way to go no nested vs nested
here`s my code  


PHP Code:
$routes->group('api', ['namespace' => 'App\Controllers\Api'], function ($routes) {
$routes->get('x/new''X::new');
    $routes->post('x/create''X::create');
    $routes->post('x''X::create');  // alias
    $routes->get('x''X::index');
    $routes->get('x/show/(:segment)''X::show/$1');
    $routes->get('x/(:segment)''X::show/$1');  // alias
    $routes->get('x/edit/(:segment)''X::edit/$1');
    $routes->post('x/update/(:segment)''X::update/$1');
    $routes->get('x/remove/(:segment)''X::remove/$1');
    $routes->post('x/delete/(:segment)''X::update/$1');

    $routes->get('x/(:segment)/y/new''Y::new/$1');
    $routes->post('x/(:segment)/y''Y::create/$1');
    $routes->get('x/(:segment)/y''Y::index/$1');
    $routes->get('x/(:segment)/y/(:segment)''Y::show/$1/$1');
    $routes->get('x/(:segment)/y/(:segment)/edit''Y::edit/$1/$1');
    $routes->put('x/(:segment)/y/(:segment)''Y::update/$1/$1');
    $routes->patch('x/(:segment)/y/(:segment)''Y::update/$1/$1');
    $routes->delete('x/(:segment)/y/(:segment)''Y::delete/$1/$1');
}); 
Reply

#3
I think with new define:
- only one url: yoursite.com/api/v1
- only one POST request
- define data post:
- ------ type [get, put, patch, list, sort, add, delete,... anything you want]
- ------ data,...
On your api controller handle, map type with function, then process and return,...

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply

#4
Only one route post

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply

#5
(10-23-2020, 06:18 AM)nc03061981 Wrote: Only one route post
 i almost finished api and i test with postman it need other route too   .
post function is not enough for curd restful api
Reply

#6
You can define data post with id,... anything

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply

#7
(10-23-2020, 06:09 PM)nc03061981 Wrote: You can define data post with id,... anything
yes i use id and restful api recommend use id too 
today  i finished backend   after i make sure every thing work i ll start to do front end with angular 10
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.