Welcome Guest, Not a member yet? Register   Sign In
Need help understanding the docs on RESTFul?
#1

I'm trying to get my head around the RESTFul part of the docs. But is confused as to where the individual parts go ...

It starts of by giving this example for setting basic routes:
Code:
$routes->resource('photos');

Now, where does this go? In the \App\Config\Routes.php file, or maybe in the (resource)controller itself? Please be specific. If it's the case it has to go into the Routes.php file, then this leads me to my next question ... does this 'port' well? If I have to scatter specific elements for specific controllers a bit here and a bit there, I guess it becomes 'unmanageable' in the long term and especially if an application becomes ever so slightly complex.

Wouldn't we need a way of somehow 'grouping' these things into a single place (thinking module here) somehow? So that, if moving 'modules' between applications we wouldn't have to 'remember' where we put all these little but VERY IMPORTANT pieces (like routes for the controller in the routes config file ... )?

Maybe I'm just not getting it, or seeing to many problems down the road. Maybe someone could provide a COMPLETE example (a very basic one, though) of how to set this up, to have a fully functioning RESTFul API – that is, the auto generated stuff?
Reply
#2

(08-24-2020, 02:34 PM)blaasvaer Wrote: I'm trying to get my head around the RESTFul part of the docs. But is confused as to where the individual parts go ...

It starts of by giving this example for setting basic routes:
Code:
$routes->resource('photos');

Now, where does this go? In the \App\Config\Routes.php file, or maybe in the (resource)controller itself? Please be specific. If it's the case it has to go into the Routes.php file, then this leads me to my next question ... does this 'port' well? If I have to scatter specific elements for specific controllers a bit here and a bit there, I guess it becomes 'unmanageable' in the long term and especially if an application becomes ever so slightly complex.

Wouldn't we need a way of somehow 'grouping' these things into a single place (thinking module here) somehow? So that, if moving 'modules' between applications we wouldn't have to 'remember' where we put all these little but VERY IMPORTANT pieces (like routes for the controller in the routes config file ... )?

There are ways to group routes - https://codeigniter4.github.io/userguide/incoming/routing.html#grouping-routes

M
y own restful API back-end is getting bigger and bigger. Now about 10 controllers with 20+ models and entities.
To handle this route-wise, I am using groups like this so I don't have to configure the filter etc for each route entry.
PHP Code:
$routes->group('config', ['except' => ['new''edit'], 'filter' => 'bearer-auth:conf'], function ($routes) {

    $routes->get('(:segment)/(:segment)/changehistory''config\Changehistory::$1/$2');

    $routes->resource('credits', ['controller' => 'config\Credits']);

    // Routes for certifications
    $routes->options('certifications/(:segment)/logo''Options::index');
    $routes->post('certifications/(:segment)/logo''config\Certifications::postCertLogo/$1');
    $routes->post('certifications/(:segment)/icon''config\Certifications::postCertIcon/$1');
    $routes->delete('certifications/(:segment)/logo''config\Certifications::deleteCertLogo/$1');
    $routes->delete('certifications/(:segment)/icon''config\Certifications::deleteCertIcon/$1');

    $routes->get('certifications/(:segment)/exams''config\Certifications::getCertExams/$1');
    $routes->get('certifications/(:segment)/issuedcerts''config\Certifications::getIssuedCerts/$1');
    $routes->get('certifications/(:segment)/certverdownload/(:segment)''config\Certifications::getCertVerDownload/$1/$2');
    $routes->delete('certifications/(:segment)/certverdownload/(:segment)''config\Certifications::deleteCertVerDownload/$1/$2');
    $routes->post('certifications/(:segment)/certverdownload/(:segment)''config\Certifications::postCertVerDownload/$1/$2');

    $routes->get('certifications/(:segment)/certversions''config\Certifications::getCertVersions/$1');
    $routes->put('certifications/(:segment)/certversions/(:segment)''config\Certifications::updateCertVersion/$1/$2');
    $routes->delete('certifications/(:segment)/certversions/(:segment)''config\Certifications::deleteCertVersion/$1/$2');
    $routes->post('certifications/(:segment)/certversions''config\Certifications::createCertVersion/$1');

    $routes->resource('certifications', ['controller' => 'config\Certifications']);

}); 
Reply
#3

(08-25-2020, 01:22 AM)tgix Wrote: There are ways to group routes - https://codeigniter4.github.io/userguide/incoming/routing.html#grouping-routes

M
y own restful API back-end is getting bigger and bigger. Now about 10 controllers with 20+ models and entities.
To handle this route-wise, I am using groups like this so I don't have to configure the filter etc for each route entry.
PHP Code:
$routes->group('config', ['except' => ['new''edit'], 'filter' => 'bearer-auth:conf'], function ($routes) {

    $routes->get('(:segment)/(:segment)/changehistory''config\Changehistory::$1/$2');

    $routes->resource('credits', ['controller' => 'config\Credits']);

    // Routes for certifications
    $routes->options('certifications/(:segment)/logo''Options::index');
    $routes->post('certifications/(:segment)/logo''config\Certifications::postCertLogo/$1');
    $routes->post('certifications/(:segment)/icon''config\Certifications::postCertIcon/$1');
    $routes->delete('certifications/(:segment)/logo''config\Certifications::deleteCertLogo/$1');
    $routes->delete('certifications/(:segment)/icon''config\Certifications::deleteCertIcon/$1');

    $routes->get('certifications/(:segment)/exams''config\Certifications::getCertExams/$1');
    $routes->get('certifications/(:segment)/issuedcerts''config\Certifications::getIssuedCerts/$1');
    $routes->get('certifications/(:segment)/certverdownload/(:segment)''config\Certifications::getCertVerDownload/$1/$2');
    $routes->delete('certifications/(:segment)/certverdownload/(:segment)''config\Certifications::deleteCertVerDownload/$1/$2');
    $routes->post('certifications/(:segment)/certverdownload/(:segment)''config\Certifications::postCertVerDownload/$1/$2');

    $routes->get('certifications/(:segment)/certversions''config\Certifications::getCertVersions/$1');
    $routes->put('certifications/(:segment)/certversions/(:segment)''config\Certifications::updateCertVersion/$1/$2');
    $routes->delete('certifications/(:segment)/certversions/(:segment)''config\Certifications::deleteCertVersion/$1/$2');
    $routes->post('certifications/(:segment)/certversions''config\Certifications::createCertVersion/$1');

    $routes->resource('certifications', ['controller' => 'config\Certifications']);

}); 

Thanks. I'm sure this is cool, but I'm taking baby steps here at the moment. I'm only just getting my head around all these 'automated' features in vCI4 ... and the docs seem to assume a lot, so it's a bit of a steep hill to climb ... but I'll keep this in the back of my head when I get advanced enough to group routes in a more 'clever' way.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB