AltoRouter
AltoRouter copied to clipboard
Need config file writing approuval
Hello, I've worked on array acceptance on the config file. Now it seems working with a config file like this :
home : {'GET|POST', '/', 'home#index'};
: {'GET', '/users/', ['c' => 'UserController', 'a' => 'ListAction']}
users_show : {'GET', '/users/[i:id]', 'users#show'}
users_do : {'POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction'}
I hope you will enjoy that addition . Thanks in advance Emmanuel
What would the benefit of being able to define routes in a config file this way. Am I missing something as to me it's just another way to format the parameters of $router->map() method.
But I did not look too deeply at the code, I just think that loading routes from a file could be achieved with a wrapper function or something that generates the routes
In my case it was helpfull. My project was initialy made with no router and I can't generate any reversed route by a algorythm handmade. So one of my Url Object load EdgeRouter from the config file, and I can generate them in order to passthrough the rewriting of Apache. I think that a wrapper function is needed if the program is ordered on it. Nevertheless this is what I do on my project, but I need the configfile to work well and to reduce the code.
In a stand alone object like AltoRouter it can be a plus . But it was my opinion.
I'm sure you find it useful, but I fail to see the difference as you basically reformat a php array into yaml and use that to generate the route map. So what is the difference you giving yaml config file to the router instead of giving an array to addroutes method?
$router->addRoutes(array(
array('PATCH','/users/[i:id]', 'users#update', 'update_user'),
array('DELETE','/users/[i:id]', 'users#delete', 'delete_user')
));
ok... I didn't know that I can use AltoRouter with this array parameters.
Nevertheless, I had 73 routes to write at this time and 200+ with the PRG pattern. Writing mulitples array() like this will be at least annoying. That simplify my mindset to use a config file.
Maybe with a extract of my first attempt with the config file, you will see that it keep it simple :
Breves : {/breves, Breves}
BreveReaction_comment : {/reagir_a_la_[i:id]eme_breve_avec_[Facebook|DISQUS:typeCommentaires]/[a:title], BreveReaction}
BreveEnMarronageReaction_comment : {/reagir_a_la_[i:id]eme_breve_marronee_avec_[Facebook|DISQUS:typeCommentaires]/[a:title], BreveEnMarronageReaction}
BreveEnArchiveReaction_comment : {/reagir_a_la_[i:id]eme_breve_archivee_avec_[Facebook|DISQUS:typeCommentaires]/[a:title], BreveEnArchiveReaction}
But my routes are in french !
I find the yaml file difficult to read (maybe it's just me :-) ), but it's basically a one-liner to turn a plain csv file for example into an array that can be fed to the altorouter method. So I think using yaml just adds some unnecessary complexity, that is probably my main issue. Although it is not a big deal to turn it into array form for the router.
For me it is very easy to read because in french when we use parenthesis multiples times into : we use in order {}, [] then (). In fact it's a 'model file' who it is Yaml and JSON inspired in one line. I've created it for my Framework SAND The only thing it have from Yaml is the name of the parameter before the colon and other things come from JSON.
May a config file with just JSON or only Yaml will be more useful for you, because model file is one of my invention :-) What do you think about this ?
Do you wish implement JSON or YAML configfile into AltoRouter ? It can be cool to do !
So, the reason of what I've renamed the class is because of the use of a 'complex' config file :-)
I don't think my current use of the router would benefit that much of any external way of configuring the routes currently.
I use the router for defining a limited set of API endpoints so not much need for dynamic routes for me.
But as I said with 4 parameters for the method to add a route even a simple csv file would be enough at least for me. It reminds me of a system I made with spreadsheet to generate csv files dynamically from user generated content.
If I had to choose, it would be csv, json, yaml in that order.
Also to not make a copy of the class you could have tried maybe inherit the class and add the functionality on top?
Hello,
I don't see this ending up in AltoRouter itself as the majority of users won't be using YAML but I am happy to list this as an available extension in the documentation.
You could leave it separate from the base class entirely and just call $altorouter->addRoutes($arrayFromYaml); after your YAML to array conversion, or you could extend the base AltoRouter class with your modifications. The former method is probably cleaner.
Hello, I need your point of view about the implementation of CSV, pure YAML and JSON files
How about this type of config file writing :
CSV config File
'GET|POST', '/', 'home#index','home'
'GET', '/users/', ['c' => 'UserController', 'a' => 'ListAction']
'GET', '/users/[i:id]', 'users#show','users_show'
'POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction','users_do'
YAML config File
routes:
- home:
- 'GET|POST'
- '/'
- 'home#index'
- :
- 'GET'
- '/users/'
- :
- c : 'UserController'
- a : 'ListAction'
- users_show:
- 'GET'
- '/users/[i:id]'
- 'users#show'
- users_do:
- 'POST'
- '/users/[i:id]/[delete|update:action]'
- 'usersController#doAction'
...
And
JSON config File
{
"routes": {
"home": [
"GET|POST",
"/",
"home#index"
],
"": [
"GET",
"/users/",
{ "c": "UserController", "a": "ListAction" }
],
"users_show": [
"GET",
"/users/[i:id]",
"users#show"
],
"users_do": [
"POST",
"/users/[i:id]/[delete|update:action]",
"usersController#doAction"
]
}
}
Can you help me about a good writing of them ? I hope it will be usefull.