lonnieezell / Bonfire

Jumpstart your CodeIgniter web applications with a modular, HMVC-ready, backend.
http://cibonfire.com
1.39k stars 524 forks source link

RESTful routes #1308

Open cosmomathieu opened 6 years ago

cosmomathieu commented 6 years ago

I'm having a bit of issue figuring out how to configure a route using BF 0.7.

What I want to accomplish: Map rest/v1/posts to modules/blog/controllers/api/posts

What I've tried:

Route::prefix('rest/v1', function(){
    Route::any('blog/api/posts', array('as' => 'posts'));
});
cosmomathieu commented 5 years ago

Here is how I solved this problem. Ugly, yes, but it works.

What I was trying to do was route site.com/rest/v1/document-manager to /application/modules/documentmanager/controllers/api/ I modified the /application/config/routes.php file and added

/**
 * Custom API routing for DocumentManager module 
 * 
 * Maps `rest/v1/documentmanager/export-to-xml` to 
 * application/modules/documentmanager/api/ExportToXml` or 
 * any other routes that matches this pattern.
 * 
 * @author Cosmo Mathieu 
 */ 
$request = array_keys($_REQUEST);
if(isset($request[0]) && strpos($request[0], 'rest/v1/documentmanager') !== false){
    $segments = explode('/', trim($request[0], '/'));

    $controller = '';
    foreach(explode('-', $segments[3]) as $bit){
        $controller .= ucfirst($bit);
    }

    Route::any('rest/v1/documentmanager/(:any)/(:any)', 'documentmanager/api/'.$controller.'/$2');
    unset($controller, $segments);
}

This may be a limitation of CI, BF, or just me, but the following did not work

Route::any('documentmanager/file.csv', 'documentmanager/file');
Route::any('rest/v1/documentmanager/(:any)/(:any)', 'documentmanager/api/$1/$2');

Hope this helps someone else!