This is a recipe not an issue.
const Route = use('Route')
const Helpers = use('Helpers')
const Ioc = require('adonis-fold').Ioc
Route.any('/:controller/:action', function * (request, response) {
const controller = request.param('controller')
const action = request.param('action')
const controllerPath = `${Helpers.appNameSpace()}/Http/Controllers`
const controllerInstance = Ioc.makeFunc(`${controllerPath}/${controller}.${action}`)
yield controllerInstance.instance[controllerInstance.method](request, response)
})
This makes infinite sense, convention over configuration. +1 more recipes.
@thetutlage Can you put suchlike recipes on a page in the documentation? I think something like that could also be community-driven :wink:
I'm curious, why would I want to use or implement this?
@vkeusebio If I'm not mistaken, this code eliminates the need to manually define routes. Everything would automatically be hooked to controller methods.
@devdebonair @vkeusebio Yes, It's will try to match every url to "controller/action" automatically
This example code below is working on AdonisJS 3.0.*
const Route = use('Route')
const Helpers = use('Helpers')
const Ioc = require('adonis-fold').Ioc
Route.on('/').render('welcome')
Route.get('/post-index', 'PostController.index')
Route.any('/:controller/:action', function * (request, response) {
const controller = request.param('controller')
const action = request.param('action')
const controllerPath = `${Helpers.appNameSpace()}/Http/Controllers`
const controllerInstance = Ioc.makeFunc(`${controllerPath}/${controller}Controller.${action}`)
yield controllerInstance.instance[controllerInstance.method](request, response)
})
If you want any other custom route, you have to add it before like in the code above.
And I've have to change
const controllerInstance = Ioc.makeFunc(${controllerPath}/${controller}.${action})
to
const controllerInstance = Ioc.makeFunc(${controllerPath}/${controller}Controller.${action})
Note: Needs the Controller keyword to event/index work instead of eventcontroller/index
How would this work for routes such as Routes.get('/users', 'UserController.index') and Routes.post('/users', 'UserController.create') ?
Hey @jamesAtcodeninja 馃憢
If you use this code the route will be /users/index & /users/create.
In case anyone is wondering here's how to do it in version 4
Route.any('/:module/:controller/:action', ({view ,request, response,params}) => {
const module = params.module
const controller = params.controller
const action = params.action
const controllerPath = `App/Controllers/Http/${module}`
const url = `${controllerPath}/${controller}.${action}`
const controllerInstance = ioc.makeFunc(url)
return controllerInstance.method.apply(controllerInstance.instance,[{view,request,response}])
})
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
This example code below is working on AdonisJS 3.0.*
const Route = use('Route') const Helpers = use('Helpers') const Ioc = require('adonis-fold').Ioc Route.on('/').render('welcome') Route.get('/post-index', 'PostController.index') Route.any('/:controller/:action', function * (request, response) { const controller = request.param('controller') const action = request.param('action') const controllerPath = `${Helpers.appNameSpace()}/Http/Controllers` const controllerInstance = Ioc.makeFunc(`${controllerPath}/${controller}Controller.${action}`) yield controllerInstance.instance[controllerInstance.method](request, response) })If you want any other custom route, you have to add it before like in the code above.
And I've have to change
const controllerInstance = Ioc.makeFunc(${controllerPath}/${controller}.${action})to
const controllerInstance = Ioc.makeFunc(${controllerPath}/${controller}Controller.${action})Note: Needs the Controller keyword to
event/indexwork instead ofeventcontroller/index