Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

RESTful routes #1308

Open
cosmomathieu opened this issue Oct 25, 2018 · 1 comment
Open

RESTful routes #1308

cosmomathieu opened this issue Oct 25, 2018 · 1 comment

Comments

@cosmomathieu
Copy link

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
Copy link
Author

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!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant