CodeIgniter Forums
RESTful Sub Resource Handling Url - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: RESTful Sub Resource Handling Url (/showthread.php?tid=77791)



RESTful Sub Resource Handling Url - paliz - 10-19-2020

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


RE: RESTful Sub Resource Handling Url - paliz - 10-22-2020

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');
}); 



RE: RESTful Sub Resource Handling Url - nc03061981 - 10-23-2020

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,...


RE: RESTful Sub Resource Handling Url - nc03061981 - 10-23-2020

Only one route post


RE: RESTful Sub Resource Handling Url - paliz - 10-23-2020

(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


RE: RESTful Sub Resource Handling Url - nc03061981 - 10-23-2020

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


RE: RESTful Sub Resource Handling Url - paliz - 10-25-2020

(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